DNA repair
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6835 | Accepted: 3169 |
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 are 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 two characters. Note that the repaired DNA can still contain only characters 'A', 'G', 'C' and 'T'.
You are to help the biologists to repair a DNA by changing least number of characters.
Input
The following N lines gives N non-empty strings of length not greater than 20 containing only characters in "AGCT", which are the DNA segments causing inherited disease.
The last line of the test case is a non-empty string of length not greater than 1000 containing only characters in "AGCT", which is the DNA to be repaired.
The last test case is followed by a line containing one zeros.
Output
number of characters which need to be changed. If it's impossible to repair the given DNA, print -1.
Sample Input
2 AAA AAG AAAG 2 A TG TGAATG 4 A G C T AGT 0
Sample Output
Case 1: 1 Case 2: 4 Case 3: -1
Source
题意:
给出n个危险的字符串,和一个询问串,输出最少修改多少个字符使得询问串中不包含危险的字符串...
分析:
水题...
建出AC自动机在上面跑DP就好了...
f[i][j]代表第i个字符匹配到第j个节点使得前i个字符位合法字符串的最小代价...直接转移就好...
每次交完题都想默念三遍我是傻逼...
代码:
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> //by NeighThorn #define inf 0x3f3f3f3f using namespace std; const int maxn=1000+5; int n,cas,tot,head,tail,q[maxn],f[maxn][maxn]; char s[maxn],DNA[maxn]; struct trie{ int cnt,fail,nxt[4]; }tr[maxn]; inline int Map(char a){ if(a=='A') return 0; if(a=='G') return 1; if(a=='C') return 2; if(a=='T') return 3; } inline void init(void){ for(int i=0;i<=tot;i++) for(int j=0;j<4;j++) tr[i].nxt[j]=0; } inline void insert(char *DNA){ int p=0,len=strlen(DNA); for(int i=0,x;i<len;i++){ x=Map(DNA[i]); if(!tr[p].nxt[x]) tr[p].nxt[x]=++tot,tr[tot].fail=-1,tr[tot].cnt=0; p=tr[p].nxt[x]; } tr[p].cnt=1; } inline void buildACM(void){ head=tail=0,q[0]=0; while(head<=tail){ int id=q[head++],p=-1; for(int i=0;i<4;i++){ if(tr[id].nxt[i]){ if(id){ if(id){ p=tr[id].fail; while(p!=-1){ if(tr[p].nxt[i]){ tr[tr[id].nxt[i]].fail=tr[p].nxt[i]; break; } p=tr[p].fail; } if(p==-1) tr[tr[id].nxt[i]].fail=0; } else tr[tr[id].nxt[i]].fail=0; } else tr[tr[id].nxt[i]].fail=0; tr[tr[id].nxt[i]].cnt|=tr[tr[tr[id].nxt[i]].fail].cnt; q[++tail]=tr[id].nxt[i]; } else if(id) tr[id].nxt[i]=tr[tr[id].fail].nxt[i]; } } } inline int DP(void){ memset(f,inf,sizeof(f)); int ans=inf,len=strlen(s);f[0][0]=0; for(int i=0,x;i<len;i++){ x=Map(s[i]); for(int j=0;j<=tot;j++) if(f[i][j]!=inf) for(int k=0;k<4;k++) if(tr[tr[j].nxt[k]].cnt==0){ f[i+1][tr[j].nxt[k]]=min(f[i+1][tr[j].nxt[k]],f[i][j]+(x==k?0:1)); if(i==len-1) ans=min(ans,f[i+1][tr[j].nxt[k]]); } } return ans==inf?-1:ans; } signed main(void){ cas=0; while(scanf("%d",&n)&&n){ init();tr[0].fail=-1;tot=0; for(int i=1;i<=n;i++) scanf("%s",DNA),insert(DNA); buildACM();scanf("%s",s);printf("Case %d: %d ",++cas,DP()); } return 0; }
By NeighThorn