题解:
构建出trie图,令f[i][j]表示到第i个字符走到j号节点最少需要修改的字符数,然后枚举后继节点转移即可。
代码:没写caseWA了n发。。。
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 2000+5
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define for4(i,x) for(int i=head[x],y;i;i=e[i].next)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,tot,f[maxn][maxn],t[maxn][],go[maxn];
char s[maxn];
bool v[maxn];
queue<int>q;
inline int id(char ch)
{
if(ch=='A')return ;
if(ch=='G')return ;
if(ch=='C')return ;
if(ch=='T')return ;
}
inline void insert()
{
memset(s,,sizeof(s));
scanf("%s",s+);int len=strlen(s+),now=;
for1(i,len)
{
int x=id(s[i]);
if(!t[now][x])t[now][x]=++tot;
now=t[now][x];
}
v[now]=;
}
void bfs()
{
q.push();
while(!q.empty())
{
int x=q.front(),y,j;v[x]|=v[go[x]];q.pop();
for0(i,)
{
j=go[x];
while(j&&!t[j][i])j=go[j];
if(t[x][i])
{
go[y=t[x][i]]=j?t[j][i]:;
q.push(y);
}else t[x][i]=j?t[j][i]:;
}
}
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int cs=;
while(cin>>n&&n)
{
tot=;
memset(t,,sizeof(t));
memset(v,,sizeof(v));
memset(go,,sizeof(go));
for1(i,n)insert();
bfs();
memset(s,,sizeof(s));
scanf("%s",s+);n=strlen(s+);
for0(i,n)for1(j,tot)f[i][j]=inf;
f[][]=;
for0(i,n-)
for1(j,tot)
if(f[i][j]!=inf)
for0(k,)
if(!v[t[j][k]])
f[i+][t[j][k]]=min(f[i+][t[j][k]],f[i][j]+(k!=id(s[i+])));
int ans=inf;
for1(i,tot)ans=min(ans,f[n][i]);
printf("Case %d: %d\n",++cs,ans==inf?-:ans);
}
return ;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5712 | Accepted: 2677 |
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