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 is 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-characters. Note that the repaired DNA can still contain only characters ' A ', ' G ', ' C ' and ' T '.
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 Inhe rited diseases.
The following n lines gives n non-empty strings of length not greater than all containing only characters in "AGCT", which is the DNA segments causing inherited disease.
The last line of the that test case is a non-empty string of length not greater than-containing only characters in "AGCT", Which is the DNA to be repaired.
The last test was 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
2aaaaagaaag2atgtgaatg4agctagt0
Sample Output
Case 1:1case 2:4case 3:-1
DP Roaming
#include <iostream>#include<queue>#include<string>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intlo=4, nu=1005; inlineintFCharu) { if(u=='A')return 0;Else if(u=='C')return 1;Else if(u=='G')return 2;Else if(u=='T')return 3;}structtree{intF; BOOLW; intT[lo]; intV[lo];} T[NU];intN,m,p,num;BOOLUs[nu];Chars[10000];queue<int>Q;intdp[1005][nu];inlineBOOLDfsintx) { if(x==0)return 0; if(T[X].W)return 1; if(Us[x])returnT[X].W; US[X]=1; returnt[x].w|=DFS (T[X].F);} Inlinevoid inch(){ intp=0, l,m=strlen (s); for(RegisterintI=0; i<m;i++) {L=f (s[i]); if(!t[p].t[l]) t[p].t[l]=++num; P=T[p].t[l]; } T[P].W=1;} InlinevoidMafa () {RegisterintIintk,p; Q.push (0); t[0].f=0; while(!Q.empty ()) {k=Q.front (); Q.pop (); for(i=0; i<lo;i++) if(T[k].t[i]) {p=t[k].f; while((!t[p].t[i]) &&p) p=t[p].f; T[T[K].T[I]].F= (k==p)?0: T[p].t[i]; Q.push (T[k].t[i]); } }}Const intinf=1e9;intMain () {inttt=0; Registerinti,j,k,l;intUintans; for(;;) {TT++; scanf ("%d",&N); if(!n)return 0; Num=u=ans=0; for(i=0; i<n;i++) scanf ("%s", s),inch(); Mafa (); for(i=0; i<=num;i++) us[i]=0; for(i=0; i<=num;i++) T[I].W|=DFS (i); for(i=0; i<=num;i++) for(j=0; j<lo;j++){ if(!T[i].t[j]) {u=t[i].f; while(!t[u].t[j]&&u) u=t[u].f; U=T[u].t[j]; }Elseu=T[i].t[j]; T[I].V[J]=u; } scanf ("%s", s); N=strlen (s); for(i=0; i<=n;i++) for(j=0; j<=num;j++) dp[i][j]=INF; dp[0][0]=0; for(i=0; i<n;i++) for(j=0; j<=num;j++) if(dp[i][j]!=INF) for(l=0; l<lo;l++) if(!T[T[J].V[L]].W)if(dp[i+1][t[j].v[l]]>dp[i][j]+ (L==f (S[i])?0:1)) Dp[i+1][t[j].v[l]]=dp[i][j]+ (L==f (S[i])?0:1); Ans=INF; for(j=0; j<=num;j++)if(Dp[n][j]<ans) ans=Dp[n][j]; if(Ans==inf) ans=-1; printf ("Case %d:%d\n", Tt,ans); for(i=0; i<=num;i++) for(j=0; j<lo;j++) t[i].t[j]=t[i].v[j]=0; for(i=0; i<=num;i++) t[i].w=t[i].f=0; }}
View Code
HDU 2457:dna Repair