传送门:点击打开链接
DNA repair
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1450 Accepted Submission(s): 792
Problem 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.
You are to help the biologists to repair a DNA by changing least number of characters.
Input
The input consists of multiple test cases. Each test case starts with a line containing one integers N (1 ≤ N ≤ 50), which is the number of DNA segments causing inherited diseases.
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.
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
For each test case, print a line containing the test case number( beginning with 1) followed by the
number of characters which need to be changed. If it's impossible to repair the given DNA, print -1.
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个病毒DNA,和一个需要修复的DNA,将需要修复的DNA的一些碱基进行修改,使其不包含上述的病毒DNA,问最少需要修改几个碱基 。
思路:将病毒DNA建成AC自动机。用dp[I][j]表示修复到需要修复的DNA的第I位,在AC自动机的节点j上,所需的最少修复次数。进行DP转移时,枚举接下来会是四个碱基中的一种,在AC自动机上跳转后,如果为跳转后的节点被标记为病毒,直continue.否则进行dp转移。
最后统计一下dp[len][I]的最小值即可
代码:
#include<cstdio>
#include<cstring>
#define SIGMA_SIZE 4
#define maxn 2000
#include<queue>
using namespace std;
int ch[maxn][SIGMA_SIZE];
int val[maxn];
int last[maxn], f[maxn];
int cnt;
inline int idx(char c)
{
if(c=='A') return 0;
else if(c=='C') return 1;
else if(c=='G') return 2;
else return 3;
}
void insert(char s[])
{
int len = strlen(s);
int u = 0;
for (int i = 0; i<len; i++)
{
int v = idx(s[i]);
if (!ch[u][v]) ch[u][v] = ++cnt;
u = ch[u][v];
}
val[u] = 1;
}
int dp[1005][1005];
void slove(char s[])
{
memset(dp,0x7F,sizeof(dp));
int len=strlen(s);
dp[0][0]=0;
for(int i=0;i<len;i++)
for(int j=0;j<=cnt;j++)
{
if(dp[i][j]==0x7F7F7F7F) continue;
int c=idx(s[i]);
for(int k=0;k<4;k++)
{
int p=j;
while(p&&!ch[p][k]) p=f[p];
p=ch[p][k];
if(val[p]) continue;
if(last[p]) continue;
int d=0;
if(k!=c) d=1;
if(dp[i][j]+d<dp[i+1][p]) dp[i+1][p]=dp[i][j]+d;
}
}
int ans=0x7F7F7F7F;
for(int i=0;i<=cnt;i++) if(dp[len][i]<ans) ans=dp[len][i];
if(ans==0x7F7F7F7F) printf("-1\n");
else printf("%d\n",ans);
}
void getFail()
{
queue<int>q;
f[0] = 0;
for (int c = 0; c<SIGMA_SIZE; c++)
{
int u = ch[0][c];
if (u)
{
f[u] = 0;
q.push(u);
last[u] = 0;
}
}
while (!q.empty())
{
int r = q.front();
q.pop();
for (int c = 0; c<SIGMA_SIZE; c++)
{
int u = ch[r][c];
if (!u)
{
//ch[r][c] = ch[f[r]][c];
continue;
}
q.push(u);
int v = f[r];
while (v&&!ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
}
int main()
{
int n;
int ks=1;
while (scanf("%d", &n),n)
{
memset(ch, 0, sizeof(ch));
memset(val, 0, sizeof(val));
cnt = 0;
char s[1005];
for (int i = 1; i <= n; i++)
{
scanf("%s", s);
insert(s);
}
getFail();
scanf("%s",s);
printf("Case %d: ",ks++);
slove(s);
}
return 0;
}