Time limit 2000 ms
Memory limit 65536 kB
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 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.
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.
题目大意
给定n个病毒序列和一个DNA序列,求最少改动DNA序列的多少个字符使得任何一个病毒序列都不是它的子串,若无法做到则输出-1
题目分析
用所有病毒序列构造出AC自动机,并按套路标记所有危险节点(单词结点)
d
p
[
i
]
[
j
]
dp[i][j]
dp[i][j]表示修改完DNA的前
i
i
i位,且当前在AC自动机的
j
j
j节点上 的 最少修改次数
初始化
d
p
[
0
]
[
0
]
=
0
dp[0][0]=0
dp[0][0]=0,其余为INF
若
S
[
i
+
1
]
=
=
c
h
[
i
]
[
k
]
S[i+1]==ch[i][k]
S[i+1]==ch[i][k],则
d
p
[
i
+
1
]
[
c
h
[
j
]
[
k
]
]
=
m
i
n
(
d
p
[
i
+
1
]
[
c
h
[
j
]
[
k
]
]
,
d
p
[
i
]
[
j
]
)
dp[i+1][\ ch[j][k]\ ]=min(dp[i+1][\ ch[j][k]\ ],dp[i][j])
dp[i+1][ ch[j][k] ]=min(dp[i+1][ ch[j][k] ],dp[i][j])
若
S
[
i
+
1
]
!
=
c
h
[
i
]
[
k
]
S[i+1]!=ch[i][k]
S[i+1]!=ch[i][k],则
d
p
[
i
+
1
]
[
c
h
[
j
]
[
k
]
]
=
m
i
n
(
d
p
[
i
+
1
]
[
c
h
[
j
]
[
k
]
]
,
d
p
[
i
]
[
j
]
+
1
)
dp[i+1][\ ch[j][k]\ ]=min(dp[i+1][\ ch[j][k]\ ],dp[i][j]+1)
dp[i+1][ ch[j][k] ]=min(dp[i+1][ ch[j][k] ],dp[i][j]+1)
最后找到 d p [ l e n ] [ i ] dp[len][i] dp[len][i]的最小值即可
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long lt;
typedef double dd;
int read()
{
int f=1,x=0;
char ss=getchar();
while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
return f*x;
}
const int inf=1128481603;
const int maxn=2010;
int n,cnt,cs;
int ch[maxn][5],rem[maxn];
int fail[maxn],dp[maxn][maxn];
queue<int> q;
char pos[]="ACGT";
char pt[maxn],txt[maxn];
void init()
{
cnt=0;
memset(ch,0,sizeof(ch));
memset(rem,0,sizeof(rem));
memset(dp,67,sizeof(dp));
memset(fail,0,sizeof(fail));
}
int get(char c)
{
for(int i=0;i<4;++i)
if(pos[i]==c) return i;
}
void ins(char* ss,int len)
{
int u=0;
for(int i=0;i<len;++i)
{
int x=get(ss[i]);
if(!ch[u][x]) ch[u][x]=++cnt;
u=ch[u][x];
}
rem[u]=1;
}
void ACM()
{
for(int i=0;i<4;++i)
if(ch[0][i]) fail[ch[0][i]]=0,q.push(ch[0][i]);
while(!q.empty())
{
int u=q.front(); q.pop();
for(int i=0;i<4;++i)
{
if(!ch[u][i]) ch[u][i]=ch[fail[u]][i];
else{
fail[ch[u][i]]=ch[fail[u]][i];
rem[ch[u][i]]|=rem[fail[ch[u][i]]];
q.push(ch[u][i]);
}
}
}
}
int DP()
{
int len=strlen(txt+1);
dp[0][0]=0;
for(int i=0;i<len;++i)
for(int j=0;j<=cnt;++j)
{
if(dp[i][j]==inf) continue;
for(int k=0;k<4;++k)
{
if(rem[ch[j][k]]) continue;
dp[i+1][ch[j][k]]=min(dp[i+1][ch[j][k]],dp[i][j]+(txt[i+1]!=pos[k]));
}
}
int ans=inf;
for(int i=0;i<=cnt;++i)
ans=min(ans,dp[len][i]);
return ans==inf?-1:ans;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0) break; init();
for(int i=1;i<=n;++i)
{
scanf("%s",&pt);
ins(pt,strlen(pt));
}
ACM();
scanf("%s",txt+1);
printf("Case %d: %d\n",++cs,DP());
}
return 0;
}