DNA repair
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2875 Accepted Submission(s): 1536
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
Recommend
teddy
题意:给出n个病毒DNA,和一个需要修复的DNA,将需要修复的DNA的一些碱基进行修改,使其不包含上述的病毒DNA,问最少需要修改几个碱基 。
解题思路:将病毒DNA建成AC自动机。用dp[i][j]表示修复到需要修复的DNA的第i位,在AC自动机的节点j上,所需的最少修复次数。最后统计一下答案即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
char ch[1005];
int n;
int dp[1200][1200];
struct Trie
{
int next[1200][4], fail[1200], flag[1200];
int root, tot;
int newnode()
{
for (int i = 0; i < 4; i++) next[tot][i] = -1;
flag[tot++] = 0;
return tot - 1;
}
void init()
{
tot = 0;
root = newnode();
}
int get(char ch)
{
if (ch == 'A')return 0;
if (ch == 'C')return 1;
if (ch == 'G')return 2;
else return 3;
}
void insert(char ch[])
{
int k = root;
for (int i = 0; ch[i]; i++)
{
if (next[k][get(ch[i])] == -1) next[k][get(ch[i])] = newnode();
k = next[k][get(ch[i])];
}
flag[k] = 1;
}
void build()
{
queue<int>q;
fail[root] = root;
for (int i = 0; i < 4; i++)
{
if (next[root][i] == -1) next[root][i] = root;
else
{
fail[next[root][i]] = root;
q.push(next[root][i]);
}
}
while (!q.empty())
{
int pre = q.front();
q.pop();
if (flag[fail[pre]]) flag[pre] = 1;
for (int i = 0; i < 4; i++)
{
if (next[pre][i] == -1) next[pre][i] = next[fail[pre]][i];
else
{
fail[next[pre][i]] = next[fail[pre]][i];
q.push(next[pre][i]);
}
}
}
}
int solve(char ch[])
{
int len = strlen(ch);
for (int i = 0; i <= len; i++)
for (int j = 0; j < tot; j++)
dp[i][j] = INF;
dp[0][0] = 0;
for (int i = 0; i < len; i++)
for (int j = 0; j < tot; j++)
if (dp[i][j] < INF)
{
for (int k = 0; k < 4; k++)
{
if (flag[next[j][k]]) continue;
int temp = (get(ch[i]) == k ? dp[i][j] : dp[i][j] + 1);
dp[i + 1][next[j][k]] = min(dp[i + 1][next[j][k]], temp);
}
}
int ans = INF;
for (int i = 0; i < tot; i++) ans = min(ans, dp[len][i]);
if (ans == INF) return -1;
else return ans;
}
void debug()
{
for (int i = 0; i < tot; i++)
{
printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], flag[i]);
for (int j = 0; j < 26; j++) printf("%2d", next[i][j]);
printf("]\n");
}
}
}ac;
int main()
{
int cas = 0;
while (~scanf("%d", &n) && n)
{
ac.init();
for (int i = 1; i <= n; i++)
{
scanf("%s", ch);
ac.insert(ch);
}
scanf("%s", ch);
ac.build();
printf("Case %d: %d\n", ++cas, ac.solve(ch));
}
return 0;
}