Test instructions
We learned in biology class thatthere are only A, C, T and G four fragments of DNA sequence.
After scientific discovery, theDNA sequence contains certain fragments that produce bad genes, such as fragment "ATC" is a bad fragment , then "ATCC", "CATCAA", " ATC ATC "are bad DNA sequences, these bad fragments we can call viral fragments ."
Now known as the m virus fragment , and then given a DNA string, ask if the fewest modifications are used (to change a letter in the DNA into another letter, such as A to T, But the changed letters can only be "ACTG"), so that the DNA string does not contain viral fragments .
"Data size and conventions"
1<=m<=50 virus fragment length not exceeding 20, contains only a,t,c,g letters
DNA string length not exceeding 1000, contains only A, T, C, G letters
Idea: DP on AC automaton
Determine if the virus part is the same as the previous
Set DP[I,J] To change the first letter of the original string, and now on the automaton on the minimum value of the J number of nodes
\[dp[i,x]=min\begin{cases} Dp[i-1,j] (K=ch[i]) \\dp[i-1,j]+1 (K<>ch[i]) \end{cases} \]
where x is the node number that the J node reaches after the letter K, requiring the J node to be a legitimate node
The answer is \[min (dp[len,i]) (I is a legitimate node) \]
1 ConstSArray[1..4] ofChar= ('A','C','G','T');2oo=500000000;3 varMapArray[1..1100,'A'..'T'] ofLongint;4Dp:Array[0..1100,0.. +] ofLongint;5B,f:Array[0..1100] ofLongint;6Q:Array[1..200000] ofLongint;7 N,tot,i,j,k,d,ans,num,p,cas:longint;8 ch:ansistring;9 Ten One procedurebuild; A varI,d,u:longint; - begin -u:=1; d:=Length (CH); the fori:=1 toD Do - begin - ifmap[u,ch[i]]=0 Then beginInc. (NUM); Map[u,ch[i]]:=num;End; -u:=Map[u,ch[i]]; + End; -b[u]:=1; + End; A at procedureAcauto; - varT,w,u,p,son,i:longint; - begin -t:=0; w:=1; q[1]:=1; - whileT<w Do - begin inInc (T); u:=Q[t]; - ifb[f[u]]=1 Thenb[u]:=1; to fori:=1 to 4 Do + ifMap[u,s[i]]>0 Then - begin theson:=Map[u,s[i]]; *p:=F[u]; $ ifu=1 Thenf[son]:=1Panax Notoginseng Elsef[son]:=Map[p,s[i]]; -Inc (W); q[w]:=Son; the End + Else A begin thep:=F[u]; + ifu=1 Thenmap[u,s[i]]:=1 - Elsemap[u,s[i]]:=Map[p,s[i]]; $ End; $ End; - End; - the functionmin (x,y:longint): Longint; - beginWuyi ifX<y Thenexit (x); the exit (y); - End; Wu - begin AboutAssign (input,'poj3691.in'); Reset (input); $Assign (output,'Poj3691.out'); Rewrite (output); - while notEof Do - begin -Fillchar (F,sizeof (f),0); AFillchar (b,sizeof (b),0); + Fillchar (Dp,sizeof (DP), $1f); the fori:=1 toNum Do - forj:=1 to 4 Domap[i,s[j]]:=0; $ READLN (n); the ifn=0 ThenBreak ; thenum:=1; Inc (CAS); the fori:=1 toN Do the begin - READLN (CH); in build; the End; the Acauto; About READLN (CH); thedp[0,1]:=0; d:=Length (CH); the fori:=1 toD Do the forj:=1 toNum Do + if(b[j]=0) and(dp[i-1, J]<oo) Then - fork:=1 to 4 Do the beginBayip:=Map[j,s[k]]; the ifb[p]=0 Then the begin - ifCH[I]=S[K] ThenDp[i,p]:=min (dp[i,p],dp[i-1, J]) - ElseDp[i,p]:=min (dp[i,p],dp[i-1, j]+1); the End; the End; theans:=Oo; the fori:=1 toNum Do - ifb[i]=0 Thenans:=min (ans,dp[d,i]); theWrite' Case'Cas': '); the ifAns<oo Thenwriteln (ANS) the ElseWriteln (-1);94 End; the close (input); the close (output); the End.
"POJ3691" DNA repair (ac automaton, DP)