Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6014 | Accepted: 2820 |
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
2008 Asia Hefei Regional Contest Online by USTC
题目链接:http://poj.org/problem?id=3691
题目大意:给一些坏的字符串,要求主串中不出现坏的字符串,问最少要改变几个主串中的字符
题目分析:将问题变为在 AC机上走(不走过危险结点),使得走过的路径得到的串与目标串匹配最多,建AC机的时候如果一个点失败指针指的点是坏点,那它也是坏点,相当于这个坏字符串是另一个的子串,这里还需要用Trie图优化,方便dp时候状态的转移,dp[i][j]表示主串匹配了前i个字符且自动机在j结点时最少要改变的字符数,注意dp的时候要避开坏点
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
int const MAX = 1005;
int const INF = 1 << 30;
char s[50], t[MAX];
struct AC_DP
{
int tot, root, fail[MAX], next[MAX][4];
int dp[MAX][MAX];
bool bad[MAX];
inline int chg(char ch)
{
if(ch == 'A')
return 0;
if(ch == 'T')
return 1;
if(ch == 'C')
return 2;
return 3;
}
inline int Newnode()
{
for(int i = 0; i < 4; i++)
next[tot][i] = -1;
fail[tot] = 0;
bad[tot] = false;
return tot ++;
}
inline void Init()
{
tot = 0;
root = Newnode();
}
inline void Insert(char *s)
{
int len = strlen(s);
int p = root;
for(int i = 0; i < len; i++)
{
int idx = chg(s[i]);
if(next[p][idx] == -1)
next[p][idx] = Newnode();
p = next[p][idx];
}
bad[p] = true;
}
inline void Build()
{
queue <int> q;
q.push(root);
while(!q.empty())
{
int p = q.front();
q.pop();
for(int i = 0; i < 4; i++)
{
if(next[p][i] == -1)
{
if(p == root)
next[p][i] = root;
else
next[p][i] = next[fail[p]][i];
}
else
{
if(p == root)
fail[next[p][i]] = root;
else
{
fail[next[p][i]] = next[fail[p]][i];
bad[p] |= bad[fail[p]];
}
q.push(next[p][i]);
}
}
}
}
inline int DP()
{
int tlen = strlen(t);
int ans = INF;
for(int i = 0; i <= tlen; i++)
for(int j = 0; j < tot; j++)
dp[i][j] = INF;
dp[0][0] = 0;
for(int i = 1; i <= tlen; i++)
for(int j = 0; j < tot; j++)
if(dp[i - 1][j] < INF)
for(int k = 0; k < 4; k++)
if(!bad[next[j][k]])
dp[i][next[j][k]] = min(dp[i][next[j][k]], dp[i - 1][j] + (chg(t[i - 1]) != k));
for(int j = 0; j < tot; j++)
if(!bad[j])
ans = min(ans, dp[tlen][j]);
if(ans == INF)
return -1;
return ans;
}
}ac;
int main()
{
int n, ca = 1;
while(scanf("%d", &n) != EOF && n)
{
ac.Init();
for(int i = 0; i < n; i++)
{
scanf("%s", s);
ac.Insert(s);
}
scanf("%s", t);
ac.Build();
printf("Case %d: %d\n", ca ++, ac.DP());
}
}