Test instructions: Give n strings, a large string, requiring a large string without the nth string above, at the minimum cost of change.
Idea: Dp,f[i][j] represents the first position in a large string, the minimum cost of the first J bit on an AC automaton.
1#include <algorithm>2#include <cstdio>3#include <cmath>4#include <cstring>5#include <iostream>6#include <queue>7std::queue<int>Q;8 intch[10005][5],end[10005],f[1005][1005],sz,root,fail[100005],n,tcase;9 Chars[10005];Ten intNewNode () { One for(intI=0; i<=3; i++) Ach[sz][i]=-1; -end[sz]=0; -sz++; the returnsz-1; - } - voidClear () { -sz=0; root=NewNode (); + } - intNumCharc) { + if(c=='A')return 0; A Else at if(c=='T')return 1; - Else - if(c=='G')return 2; - Else - if(c=='C')return 3; - return 0; in } - voidInsert () { toscanf"%s", s); + intlen=strlen (s); - intnow=Root; the for(intI=0; i<len;i++){ * if(Ch[now][num (s[i])]==-1) Ch[now][num (S[i])]=NewNode (); $now=Ch[now][num (S[i]);Panax Notoginseng } -end[now]=1; the } + voidbuild () { A intnow=Root; thestd::queue<int>Q; +fail[root]=Root; - for(intI=0; i<=3; i++) $ if(ch[root][i]==-1) ch[root][i]=Root; $ Elsefail[ch[root][i]]=Root,q.push (Ch[root][i]); - while(!Q.empty ()) { - intnow=Q.front (); Q.pop (); the if(end[fail[now]]==1) end[now]|=1; - for(intI=0; i<=3; i++){Wuyi if(ch[now][i]==-1) ch[now][i]=Ch[fail[now]][i]; the Elsefail[ch[now][i]]=Ch[fail[now]][i],q.push (Ch[now][i]); - } Wu } - } About voiddp () { $scanf"%s", s); - intlen=strlen (s); - for(intI=0; i<=len;i++) - for(intj=0; j<=sz;j++) Af[i][j]=99999999; +f[0][root]=0; the for(intI=0; i<len;i++) - for(intj=0; j<sz;j++) $ if(f[i][j]<99999999){ the for(intk=0; k<=3; k++){ the intnow=Ch[j][k]; the if(End[now])Continue; the inttmp; - if(K==num (S[i])) tmp=F[i][j]; in Elsetmp=f[i][j]+1; thef[i+1][now]=std::min (f[i+1][now],tmp); the } About } the intans=99999999; the for(intI=0; i<sz;i++) theans=std::min (Ans,f[len][i]); + if(ans==99999999) ans=-1; -printf"%d\n", ans); the }Bayi intMain () { the while(SCANF ("%d", &n)! =EOF) { the if(n==0)return 0; -tcase++; -printf"Case %d:", Tcase); the Clear (); the for(intI=1; i<=n;i++) the Insert (); the build (); - DP (); the } the}
HDU 2457 DNA Repair (ac automaton +DP)