Test instructions: Give n viral DNA sequence, and then give a DNA sequence, ask the sequence at least how many base can be modified to contain no viral DNA. The viral DNA sequence does not exceed 1000, and the length of the DNA questioned is no more than 1000.
The dp:d[l][p] indicates that the minimum number of modifications to the L-bit, the current position on the AC automaton is P, and the d[l][p] is used to push d[l+1][x]. The original is recursive, the result is recursive dead loop. So I changed it into a recursive push.
If I don't want to be clear, I'll die of tat.
1 //poj36912#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <iostream>6#include <queue>7 using namespacestd;8 9 Const intn=1100, inf= (int) 1e9;Ten intN,num,sl,ans,d[n][n]; One CharS[n]; A structnode{ - intfail,fa,bk,son[5]; - }a[n]; thequeue<int>Q; - - intMinn (intXintY) {returnX<y?x:y;} - + intIdxCharc) - { + if(c=='A')return 1; A if(c=='T')return 2; at if(c=='C')return 3; - if(c=='G')return 4; - } - - voidClearintx) - { inA[x].bk=a[x].fail=a[x].fa=0; -memset (A[x].son,0,sizeof(A[x].son)); to } + - voidRead_trie () the { *scanf"%s", s); $ intx=0, l=strlen (s);Panax Notoginseng for(intI=0; i<l;i++) - { the intInd=idx (s[i]); + if(!A[x].son[ind]) A { theClear (+ +num); +a[x].son[ind]=num; -A[num].fa=x; $ } $x=A[x].son[ind]; - } -a[x].bk=1; the } - Wuyi voidBuildac () the { - while(!q.empty ()) Q.pop (); Wu for(intI=1; i<=4; i++) - if(a[0].son[i]) Q.push (a[0].son[i]); About while(!q.empty ()) $ { - intx=Q.front (); Q.pop (); - intFail=A[x].fail; - for(intI=1; i<=4; i++) A { + if(A[x].son[i]) the { - inty=a[x].son[i],z=A[fail].son[i]; $A[y].fail=Z; thea[y].bk|=A[Z].BK; the Q.push (y); the } the Elsea[x].son[i]=A[fail].son[i]; - } in } the } the About intMain () the { theFreopen ("a.in","R", stdin); theFreopen ("a.out","W", stdout); + intt=0; - while(1) the {Bayiscanf"%d",&n); the if(!n)return 0; thenum=0; -Clear0); - for(intI=1; i<=n;i++) Read_trie (); the Buildac (); thescanf"%s", s+1); theSl=strlen (s+1); theans=INF; -memset (D, the,sizeof(d)); thed[0][0]=0; the for(intI=0; i<sl;i++) the {94 for(intj=0; j<=num;j++)//position on the AC automaton the { the if(d[i][j]<INF) the {98 intInd=idx (s[i+1]); About intx=A[j].son[ind]; - for(intk=1; k<=4; k++)101 {102 inty=A[j].son[k];103 if(A[Y].BK | | x==y)Continue;104d[i+1][y]=minn (d[i+1][y],d[i][j]+1); the }106 if(a[x].bk==0) 107d[i+1][x]=minn (d[i+1][x],d[i][j]);108 }109 } the }111 for(intI=0; i<=num;i++) ans=Minn (ans,d[sl][i]); the if(Ans<inf) printf ("Case %d:%d\n",++T,ans);113 Elseprintf"Case %d: -1\n",++T); the } the return 0; the}
"Poj3691-dna repair" AC automatic machine +DP