The problem of a string of DNA requires at least several genes to be modified to contain no pathogenic DNA fragments.
This problem should be the introduction of AC automata +DP, there is POJ2778 Foundation is not difficult to write out.
DP[I][J] Indicates the minimum number of genes that need to be modified for the first I bit of the original DNA (transfer I step on the AC automaton) and the suffix state to the AC automaton node J
Transfer me for everyone, from Dp[i][j] to ATCG in four directions to Dp[i+1][j '] if the node is labeled to contain the causative gene, it cannot be transferred.
1#include <cstdio>2#include <cstring>3#include <queue>4 using namespacestd;5 #defineINF (1<<30)6 inttn,ch[1111][4],fail[1111],idx[ -];7 BOOLflag[1111];8 voidInsertChar*s) {9 intx=0;Ten for(intI=0; T[n]; ++i) { One inty=Idx[s[i]]; A if(ch[x][y]==0) ch[x][y]=++tn; -x=Ch[x][y]; - } theflag[x]=1; - } - voidinit () { -memset (fail,0,sizeof(fail)); +queue<int>que; - for(intI=0; i<4; ++i) { + if(ch[0][i]) Que.push (ch[0][i]); A } at while(!Que.empty ()) { - intx=Que.front (); Que.pop (); - for(intI=0; i<4; ++i) { - if(Ch[x][i]) Que.push (Ch[x][i]), fail[ch[x][i]]=Ch[fail[x]][i]; - Elsech[x][i]=Ch[fail[x]][i]; -flag[ch[x][i]]|=Flag[ch[fail[x]][i]]; in } - } to } + intd[1111][1111]; - intMain () { theidx['A']=0; idx['G']=1; idx['C']=2; idx['T']=3; * Charstr[1111]; $ intn,t=0;Panax Notoginseng while(~SCANF ("%d", &n) &&N) { -tn=0; thememset (CH,0,sizeof(CH)); +memset (Flag,0,sizeof(flag)); A while(n--){ thescanf"%s", str); + Insert (str); - } $ init (); $scanf"%s", str+1); -N=strlen (str+1); - for(intI=0; i<=n; ++i) { the for(intj=0; j<=tn; ++J) d[i][j]=INF; - }Wuyid[0][0]=0; the for(intI=0; i<n; ++i) { - for(intj=0; j<=tn; ++j) { Wu if(D[i][j]==inf | | flag[j])Continue; - for(intk=0; k<4; ++k) { About if(Flag[ch[j][k]])Continue; $ if(idx[str[i+1]]==K) d[i+1][ch[j][k]]=min (d[i+1][ch[j][k]],d[i][j]); - Elsed[i+1][ch[j][k]]=min (d[i+1][ch[j][k]],d[i][j]+1); - } - } A } + intres=INF; the for(intI=0; i<=tn; ++i) res=min (res,d[n][i]); - if(Res==inf) printf ("Case %d:%d\n", ++t,-1); $ Elseprintf"Case %d:%d\n",++t,res); the } the return 0; the}
HDU2457 DNA Repair (ac automaton +DP)