Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6758 | Accepted: 3133 |
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 last test case is followed by a line containing one zeros.
Output
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
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N=1005,INF=1e9; inline int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();} return x*f; } int n,m,mp[N]; char s[N]; struct node{ int ch[5],fail,val; }t[N]; int sz; void ins(char s[]){ int u=0,n=strlen(s+1); for(int i=1;i<=n;i++){ int c=mp[s[i]]; if(!t[u].ch[c]) t[u].ch[c]=++sz; u=t[u].ch[c]; } t[u].val=1; } int q[N],head,tail; void getAC(){ head=tail=1; for(int i=1;i<=4;i++) if(t[0].ch[i]) q[tail++]=t[0].ch[i]; while(head!=tail){ int u=q[head++]; t[u].val|=t[t[u].fail].val; for(int i=1;i<=4;i++){ int &v=t[u].ch[i]; if(!v) v=t[t[u].fail].ch[i]; else{ t[v].fail=t[t[u].fail].ch[i]; q[tail++]=v; } } } } int f[N][N]; void dp(){ memset(f,127,sizeof(f)); f[0][0]=0; for(int i=0;i<m;i++) for(int j=0;j<=sz;j++) if(f[i][j]<INF){ int k=t[j].ch[mp[s[i+1]]]; if(!t[k].val) f[i+1][k]=min(f[i+1][k],f[i][j]); for(int k=1;k<=4;k++) if(!t[t[j].ch[k]].val) f[i+1][t[j].ch[k]]=min(f[i+1][t[j].ch[k]],f[i][j]+1); } //for(int i=1;i<=m;i++) // for(int j=0;j<=sz;j++) printf("hi %d %d %d\n",i,j,f[i][j]); int ans=INF; for(int i=0;i<=sz;i++) ans=min(ans,f[m][i]); if(ans>=INF) puts("-1"); else printf("%d\n",ans); } int main(){ freopen("in","r",stdin); int cas=0; mp[‘A‘]=1;mp[‘G‘]=2;mp[‘C‘]=3;mp[‘T‘]=4; while((n=read())){ printf("Case %d: ",++cas); memset(t,0,sizeof(t));sz=0; for(int i=1;i<=n;i++) scanf("%s",s+1),ins(s); scanf("%s",s+1); m=strlen(s+1); getAC(); dp(); } }
POJ 3691 DNA repair [AC自动机 DP]
原文:http://www.cnblogs.com/candy99/p/6368292.html