hdu DNA repair(AC自动机+DP)

来源:互联网 发布:淘宝怎么指纹支付功能 编辑:程序博客网 时间:2024/06/05 03:15

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

2AAAAAGAAAG    2ATGTGAATG4AGCTAGT0

Sample Output

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


先将DNA片段建立AC自动机,然后在AC自动机上进行dp,dp[i][j]表示长度为i移动到j节点
修改了最少的步数。每次走到边如果和字符串不同,权值即为1;相同则为0。单词节点不能向后转移。

dp[i+1][u]=min(dp[i+1][u],dp[i][j]+c!=idx(s[i]));

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int maxnode=55*22;const int maxn=1010;const int kind=4;const int inf=0x7f7f7f7f;struct node{int ch[maxnode][kind],sz;int val[maxnode],fail[maxnode],last[maxnode];int dp[maxn][maxnode];void init(){sz=1;memset(ch[0],0,sizeof(ch[0]));memset(dp,inf,sizeof(dp));}int idx(char c){if(c=='A') return 0;if(c=='C') return 1;if(c=='G') return 2;if(c=='T') return 3;}void insert(char *s,int v){int u=0,n=strlen(s);for(int i=0;i<n;i++){int id=idx(s[i]);if(!ch[u][id]){memset(ch[sz],0,sizeof(ch[sz]));val[sz]=0;ch[u][id]=sz++;}u=ch[u][id];}val[u]=v;}void getFail(){queue<int>q;fail[0]=0;for(int i=0;i<kind;i++){int u=ch[0][i];if(u){fail[u]=0;q.push(u);last[u]=0;}}while(!q.empty()){int r=q.front();q.pop();for(int i=0;i<kind;i++){int u=ch[r][i];if(!u){ch[r][i]=ch[fail[r]][i];continue;}q.push(u);int v=fail[r];while(v&&!ch[v][i]) v=fail[v];fail[u]=ch[v][i];last[u]=val[fail[u]]?fail[u]:last[fail[u]];}}}void print(int x){if(x){printf("%d: %d\n",x,val[x]);print(last[x]);}}void find(char *s){int j=0,n=strlen(s);for(int i=0;i<n;i++){int id=idx(s[i]);while(j&&!ch[j][id]) j=fail[j];j=ch[j][id];if(val[j]) print(j);else if(last[j]) print(last[j]);}}int DP(char *s){int n=strlen(s);dp[0][0]=0;for(int i=0;i<n;i++){for(int j=0;j<sz;j++){if(dp[i][j]==inf||val[j]||last[j])continue;for(int k=0;k<kind;k++){int u=j;while(u&&!ch[u][k]) u=fail[u];u=ch[u][k];int d=idx(s[i])==k?0:1;dp[i+1][u]=min(dp[i+1][u],dp[i][j]+d);}}}int ans=inf;for(int i=0;i<sz;i++){if(val[i]||last[i])continue;ans=min(ans,dp[n][i]);}return ans==inf?-1:ans;}};node ac;char P[maxn];int main(){int n,Case=0;while(scanf("%d",&n)==1&&n!=0){ac.init();for(int i=1;i<=n;i++){scanf("%s",P);ac.insert(P,i);}ac.getFail();scanf("%s",P);int ans=ac.DP(P);printf("Case %d: %d\n",++Case,ans);}return 0;}


0 0
原创粉丝点击