首页 > 代码库 > poj 3691 DNA repair
poj 3691 DNA repair
Time Limit: 2000MS | Memory Limit: 65536K | |
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
2AAAAAGAAAG 2ATGTGAATG4AGCTAGT0
Sample Output
Case 1: 1Case 2: 4Case 3: -1
Source
给出n个模式串,和一个长度为m的原串,求最少修改几位,使得其中不包含任何一个模式串为子串
字母只有AGCT
dp[i][j]表示已经修改好了前i位u,当前在AC自动机的j节点,且前i位不包含任何一个模式串位子串的最小修改次数
不改,dp[i][j]=dp[i+1][k]
改 ,dp[i][j]=dp[i+1][k]+1
取min
如果dp[i+1][k]是单词节点就不能转移
记忆化搜索即可
#include<queue>#include<cstdio>#include<cstring>using namespace std;int n,len,root,id,ans;char s[1001];bool mark[51*21],v[1001][51*21];int trie[51*21][5],tot,f[51*21],dp[1001][51*21];queue<int>q;struct ACautomata{ int get(char c) { if(c==‘A‘) return 0; if(c==‘G‘) return 1; if(c==‘C‘) return 2; if(c==‘T‘) return 3; } void insert() { root=1; len=strlen(s); for(int i=0;i<len;i++) { id=get(s[i]); if(!trie[root][id]) { trie[root][id]=++tot; memset(trie[tot],0,sizeof(trie[tot])); mark[tot]=0; } root=trie[root][id]; } mark[root]=true; } void getfail() { memset(f,0,sizeof(f)); q.push(1); int now,j; while(!q.empty()) { now=q.front(); q.pop(); for(int i=0;i<4;i++) { if(!trie[now][i]) { trie[now][i]=trie[f[now]][i]; continue; } q.push(trie[now][i]); j=f[now]; f[trie[now][i]]=trie[j][i]; if(mark[trie[j][i]]) mark[trie[now][i]]=true; } } } int dfs(int l,int now) { if(l==len) return 0; if(v[l][now]) return dp[l][now]; v[l][now]=true; if(!mark[trie[now][get(s[l+1])]]) dp[l][now]=dfs(l+1,trie[now][get(s[l+1])]); else dp[l][now]=2000; for(int i=0;i<4;i++) if(!mark[trie[now][i]]&&i!=get(s[l+1])) dp[l][now]=min(dp[l][now],dfs(l+1,trie[now][i])+1); return dp[l][now]; }};ACautomata AC;int main(){ for(int i=0;i<4;i++) trie[0][i]=1; int t=0; while(scanf("%d",&n)!=EOF) { if(!n) return 0; tot=1; memset(v,0,sizeof(v)); memset(trie[1],0,sizeof(trie[1])); memset(dp,0,sizeof(dp)); while(n--) { scanf("%s",s); AC.insert(); } AC.getfail(); scanf("%s",s+1); len=strlen(s+1); ans=AC.dfs(0,1); if(ans==2000) ans=-1; printf("Case %d: %d\n",++t,ans); }}
poj 3691 DNA repair