HDU 2457 DNA repair

地址:http://acm.hdu.edu.cn/showproblem.php?pid=2457

题目:

DNA repair

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2718    Accepted Submission(s): 1456


Problem Description
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.
 

 

Input
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.
 

 

Output
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.
 

 

Sample Input
2 AAA AAG AAAG 2 A TG TGAATG 4 A G C T AGT 0
 

 

Sample Output
Case 1: 1 Case 2: 4 Case 3: -1
 

 

Source
 
思路:
  dp[i][j]表示第i个字符走到底j个节点时的使当前串合法的最小修改次数。然后转移下就好了
  注意end标记要上传。
  1 #include <queue>
  2 #include <cstring>
  3 #include <cstdio>
  4 using namespace std;
  5 
  6 const int INF=0x3f3f3f3f;
  7 struct AC_auto
  8 {
  9     const static int LetterSize = 4;
 10     const static int TrieSize = 4 * ( 1e3 + 50);
 11 
 12     int tot,root,fail[TrieSize],end[TrieSize],next[TrieSize][LetterSize];
 13     int dp[2][2000];
 14     int newnode(void)
 15     {
 16         memset(next[tot],-1,sizeof(next[tot]));
 17         end[tot] = 0;
 18         return tot++;
 19     }
 20 
 21     void init(void)
 22     {
 23         tot = 0;
 24         root = newnode();
 25     }
 26 
 27     int getidx(char x)
 28     {
 29         if(x=='A') return 0;
 30         else if(x=='C') return 1;
 31         else if(x=='G') return 2;
 32         return 3;
 33     }
 34 
 35     void insert(char *ss)
 36     {
 37         int len = strlen(ss);
 38         int now = root;
 39         for(int i = 0; i < len; i++)
 40         {
 41             int idx = getidx(ss[i]);
 42             if(next[now][idx] == -1)
 43                 next[now][idx] = newnode();
 44             now = next[now][idx];
 45         }
 46         end[now]=1;
 47     }
 48 
 49     void build(void)
 50     {
 51         queue<int>Q;
 52         fail[root] = root;
 53         for(int i = 0; i < LetterSize; i++)
 54             if(next[root][i] == -1)
 55                 next[root][i] = root;
 56             else
 57                 fail[next[root][i]] = root,Q.push(next[root][i]);
 58         while(Q.size())
 59         {
 60             int now = Q.front();Q.pop();
 61             for(int i = 0; i < LetterSize; i++)
 62             if(next[now][i] == -1)   next[now][i] = next[fail[now]][i];
 63             else
 64             {
 65                  fail[next[now][i]] = next[fail[now]][i];
 66                  end[next[now][i]]|=end[fail[next[now][i]]];
 67                  Q.push(next[now][i]);
 68             }
 69         }
 70     }
 71 
 72     int match(char *ss)
 73     {
 74         int len,now,res;
 75         len = strlen(ss),now = root,res = 0;
 76         for(int i = 0; i < len; i++)
 77         {
 78             int idx = getidx(ss[i]);
 79             int tmp = now = next[now][idx];
 80             while(tmp)
 81             {
 82                 res += end[tmp];
 83                 end[tmp] = 0;//按题目修改
 84                 tmp = fail[tmp];
 85             }
 86         }
 87         return res;
 88     }
 89 
 90     int go(char *ss)
 91     {
 92         int len=strlen(ss),now=1,pre=0,ans=INF;
 93         memset(dp,INF,sizeof dp);
 94         dp[0][0]=0;
 95         for(int i=0;i<len;i++)
 96         {
 97             for(int j=0;j<tot;j++)
 98                 dp[now][j]=INF;
 99             for(int j=0;j<tot;j++)
100             for(int k=0;k<4;k++)
101             if(!end[next[j][k]])
102                 dp[now][next[j][k]]=min(dp[now][next[j][k]],dp[pre][j]+(getidx(ss[i])==k?0:1));
103             swap(now,pre);
104         }
105         for(int i=0;i<tot;i++)
106             ans=min(ans,dp[pre][i]);
107         return ans==INF?-1:ans;
108     }
109     void debug()
110     {
111         for(int i = 0;i < tot;i++)
112         {
113             printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
114             for(int j = 0;j < LetterSize;j++)
115                 printf("%3d",next[i][j]);
116             printf("]\n");
117         }
118     }
119 };
120 AC_auto ac;
121 char ss[2000];
122 int main(void)
123 {
124     int n,cs=1;
125     while(~scanf("%d",&n)&&n)
126     {
127         ac.init();
128         while(n--)
129             scanf("%s",ss),ac.insert(ss);
130         ac.build();
131         scanf("%s",ss);
132         printf("Case %d: %d\n",cs++,ac.go(ss));
133     }
134     return 0;
135 }

 

posted @ 2017-08-28 22:58  weeping  阅读(207)  评论(0编辑  收藏  举报