DNA repair

叁歲伎倆 2022-08-18 02:26 248阅读 0赞

DNA repair

#

Time Limit: 2000ms Memory limit: 32768K 有疑问?点这里^_^

题目描述

Biologists finally invent techniques of repairing DNA that contains segments causing kinds of inherited diseases. For the sake of simplicity, a DNA is represented as a string containing characters ‘A’, ‘G’ , ‘C’ and ‘T’. The repairing techniques are simply to change some characters to eliminate all segments causing diseases. For example, we can repair a DNA “AAGCAG” to “AGGCAC” to eliminate the initial causing disease segments “AAG”, “AGC” and “CAG” by changing two characters. Note that the repaired DNA can still contain only characters ‘A’, ‘G’, ‘C’ and ‘T’.

You are to help the biologists to repair a DNA by changing least number of characters.

输入

The input consists of multiple test cases. Each test case starts with a line containing one integers N (1 ≤ N ≤ 50), which is the number of DNA segments causing inherited diseases. The following N lines gives N non-empty strings of length not greater than 20 containing only characters in “AGCT”, which are the DNA segments causing inherited disease. The last line of the test case is a non-empty string of length not greater than 1000 containing only characters in “AGCT”, which is the DNA to be repaired.

The last test case is followed by a line containing one zeros.

输出

For each test case, print a line containing the test case number( beginning with 1) followed by the number of characters which need to be changed. If it’s impossible to repair the given DNA, print -1.

示例输入

  1. 2
  2. AAA
  3. AAG
  4. AAAG
  5. 2
  6. A
  7. TG
  8. TGAATG
  9. 4
  10. A
  11. G
  12. C
  13. T
  14. AGT
  15. 0

示例输出

  1. Case 1: 1
  2. Case 2: 4
  3. Case 3: -1

提示

hdoj2457 有链接提示的题目请先去链接处提交程序,AC后提交到SDUTOJ中,以便查询存档。

来源

2008 Asia Hefei Regional Contest Online by USTC

示例程序

  • include

    1. #include<cstdio>
    2. #include<cstring>
    3. #include<cmath>
    4. #include<algorithm>
    5. #define N 100005
    6. #define MOD 100000
    7. #define inf 1<<29
    8. #define LL long long
    9. using namespace std;
    10. struct Trie{
    11. Trie *next[4];
    12. Trie *fail;
    13. int kind,isword;
    14. };
    15. Trie *que[N],s[N];
    16. int idx;
    17. int id(char ch){
    18. if(ch=='A') return 0;
    19. else if(ch=='T') return 1;
    20. else if(ch=='C') return 2;
    21. return 3;
    22. }
    23. Trie *NewNode(){
    24. Trie *tmp=&s[idx];
    25. for(int i=0;i<4;i++) tmp->next[i]=NULL;
    26. tmp->isword=0;
    27. tmp->kind=idx++;
    28. tmp->fail=NULL;
    29. return tmp;
    30. }
    31. void Insert(Trie *root,char *s,int len){
    32. Trie *p=root;
    33. for(int i=0;i<len;i++){
    34. if(p->next[id(s[i])]==NULL)
    35. p->next[id(s[i])]=NewNode();
    36. p=p->next[id(s[i])];
    37. }
    38. p->isword=1;
    39. }
    40. void Bulid_Fail(Trie *root){
    41. int head=0,tail=0;
    42. que[tail++]=root;
    43. root->fail=NULL;
    44. while(head<tail){
    45. Trie *tmp=que[head++];
    46. for(int i=0;i<4;i++){
    47. if(tmp->next[i]){
    48. if(tmp==root) tmp->next[i]->fail=root;
    49. else{
    50. Trie *p=tmp->fail;
    51. while(p!=NULL){
    52. if(p->next[i]){
    53. tmp->next[i]->fail=p->next[i];
    54. break;
    55. }
    56. p=p->fail;
    57. }
    58. if(p==NULL) tmp->next[i]->fail=root;
    59. }
    60. if(tmp->next[i]->fail->isword) tmp->next[i]->isword=1;
    61. que[tail++]=tmp->next[i];
    62. }
    63. else if(tmp==root) tmp->next[i]=root;
    64. else tmp->next[i]=tmp->fail->next[i];
    65. }
    66. }
    67. }
    68. int dp[1005][2005];
    69. int slove(char *str,int len){
    70. for(int i=0;i<=len;i++) for(int j=0;j<idx;j++) dp[i][j]=inf;
    71. dp[0][0]=0;
    72. for(int i=1;i<=len;i++){
    73. for(int j=0;j<idx;j++){
    74. if(s[j].isword) continue;
    75. if(dp[i-1][j]==inf) continue;
    76. for(int k=0;k<4;k++){
    77. int r=s[j].next[k]->kind;
    78. if(s[r].isword) continue;
    79. dp[i][r]=min(dp[i][r],dp[i-1][j]+(id(str[i-1])!=k));
    80. }
    81. }
    82. }
    83. int ans=inf;
    84. for(int i=0;i<idx;i++) ans=min(ans,dp[len][i]);
    85. return ans==inf?-1:ans;
    86. }
    87. char str[1005];
    88. int main(){
    89. int n,cas=0;
    90. while(scanf("%d",&n)!=EOF&&n){
    91. idx=0;
    92. Trie *root=NewNode();
    93. for(int i=0;i<n;i++){
    94. scanf("%s",str);
    95. Insert(root,str,strlen(str));
    96. }
    97. Bulid_Fail(root);
    98. scanf("%s",str);
    99. printf("Case %d: %d\n",++cas,slove(str,strlen(str)));
    100. }
    101. return 0;
    102. }

发表评论

表情:
评论列表 (有 0 条评论,248人围观)

还没有评论,来说两句吧...

相关阅读

    相关 DNA序列

    > 一个DNA序列由A/C/G/T四个字母的排列组合组成。G和C的比例(定义为GC-Ratio)是序列中G和C两个字母的总的出现次数除以总的字母数目(也就是序列长度)。在基因工

    相关 Fence Repair

    think: 1 今天AC了人生中第一道英文Oj题目,内心也不知如何描述,一开始提交发现wrong answer, 后来借鉴了鑫哥的代码,发现虽然和鑫哥用的方法不同,但注意

    相关 Fence Repair

    think: 1 今天AC了人生中第一道英文Oj题目,内心也不知如何描述,一开始提交发现wrong answer, 后来借鉴了鑫哥的代码,发现虽然和鑫哥用的方法不同,但注意

    相关 变色DNA

    有一只特别的狼,它在每个夜晚会进行变色,研究发现它可以变成N种颜色之一,将这些颜色标号为0,1,2...N-1。研究发现这只狼的基因中存在一个变色矩阵,记为colormap,如