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
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cctype>
#include<map>
using namespace std;
//给你一堆模板DNA,和一串DNA S,问你最少修改几处,使S不含任何模板DNA(可重叠)
const int inf=(1<<30);
const int maxn=2500;
struct Node
{
int flag;//是否合法
int id;//在静态链表中的位置
Node* next[4];
Node* fail;
};
Node temp[maxn];
int tp;
int n;
Node* root;
int hash[200];
void reset(Node* p)
{
p->flag=0;p->id=tp-1;
for(int i=0;i<4;i++) p->next[i]=NULL;
p->fail=root;
if(p==root) p->fail=NULL;
}
void init()
{
hash['A']=0,hash['T']=1,hash['G']=2,hash['C']=3;
tp=0;
root=&temp[tp++];
reset(root);
}
void insert(char* word)
{
Node* p=root;
for(int i=0;word[i];i++)
{
int x=hash[word[i]];
if(p->next[x]==NULL)
{
p->next[x]=&temp[tp++];
reset(p->next[x]);
}
p=p->next[x];
}
p->flag=1;
}
Node* que[maxn*4];
int front,rear;
void DFA()
{
Node* p=root;
front=rear=0;
que[rear++]=p;
while(front<rear)
{
Node* t=que[front++];
for(int i=0;i<4;i++)
{
Node* cnt=t->next[i];
if(cnt!=NULL)
{
Node* fath=t->fail;
while(fath!=NULL&&fath->next[i]==NULL)
{
fath=fath->fail;
}
if(fath!=NULL)
{
cnt->fail=fath->next[i];
}
else
{
cnt->fail=p;
}
que[rear++]=cnt;
}
}
}
}
char str[maxn];
int len;
int dp[maxn][maxn];
/*
dp[i][j] 表示到目标串的第 i 个字符,DFA 状态为 j 时的最小修改数量。
则 dp[i][ son[j] ]= min{ dp[i][ son[j] ], dp[i-1][j]+ (tran[j][son[j]]
!= str[i])},tran[j][ son[j] ]表示从 j 到 son[j] 经过的字母边。
*/
void calc()
{
len=strlen(str);
for(int i=0;i<=len;i++) for(int j=0;j<rear;j++) dp[i][j]=inf;
dp[0][0]=0;
Node* fath;
for(int i=1;i<=len;i++)
{
for(int j=0;j<rear;j++)
{
Node* p=&temp[j];
int f=1;//important
for(fath=p;fath!=NULL;fath=fath->fail)
{
if(fath->flag) {
f=0;
break;
}
}
if(dp[i-1][j]!=inf&&f)
{
for(int t=0;t<4;t++)
{
int k;
Node* cnt=p->next[t];
if(cnt!=NULL)
{
int mark=1;//important
for(fath=cnt;fath!=NULL;fath=fath->fail)
{
if(fath->flag) {
mark=0;
break;
}
}
if(mark)
{
k=cnt->id;
dp[i][k]=min(dp[i][k],dp[i-1][j]+(t!=hash[str[i-1]]));
}
}
else
{
fath=p->fail;
while(fath!=NULL&&fath->next[t]==NULL)
{
fath=fath->fail;
}
if(fath!=NULL)
{
cnt=fath->next[t];
int mark=1;//important
for(fath=cnt;fath!=NULL;fath=fath->fail)
{
if(fath->flag) {
mark=0;
break;
}
}
if(mark)
{
k=cnt->id;
dp[i][k]=min(dp[i][k],dp[i-1][j]+(t!=hash[str[i-1]]));
}
}
else
{
fath=root;
k=0;
dp[i][k]=min(dp[i][k],dp[i-1][j]+(t!=hash[str[i-1]]));
}
}
}
}
}
}
}
int main()
{
int pl=1;
while(scanf("%d",&n)==1&&n)
{
init();
for(int i=0;i<n;i++)
{
scanf("%s",str);
insert(str);
}
DFA();
scanf("%s",str);
calc();
int ans=inf;
for(int i=0;i<rear;i++) ans=min(ans,dp[len][i]);
printf("Case %d: ", pl++ );
if( ans== inf ) printf("-1\n");
else printf("%d\n", ans );
}
return 0;
}
//以上代码优化版
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cctype>
#include<map>
using namespace std;
//给你一堆模板DNA,和一串DNA S,问你最少修改几处,使S不含任何模板DNA(可重叠)
const int inf=(1<<30);
const int maxn=2500;
struct Node
{
int flag;//是否合法
int id;//在静态链表中的位置
Node* next[4];
Node* fail;
};
Node temp[maxn];
int tp;
int n;
Node* root;
int hash[200];
void reset(Node* p)
{
p->flag=0;p->id=tp-1;
for(int i=0;i<4;i++) p->next[i]=NULL;
p->fail=root;
if(p==root) p->fail=NULL;
}
void init()
{
hash['A']=0,hash['T']=1,hash['G']=2,hash['C']=3;
tp=0;
root=&temp[tp++];
reset(root);
}
void insert(char* word)
{
Node* p=root;
for(int i=0;word[i];i++)
{
int x=hash[word[i]];
if(p->next[x]==NULL)
{
p->next[x]=&temp[tp++];
reset(p->next[x]);
}
p=p->next[x];
}
p->flag=1;
}
Node* que[maxn*4];
int front,rear;
void DFA()
{
Node* p=root;
front=rear=0;
que[rear++]=p;
while(front<rear)
{
Node* t=que[front++];
for(int i=0;i<4;i++)
{
Node* cnt=t->next[i];
if(cnt!=NULL)
{
Node* fath=t->fail;
while(fath!=NULL&&fath->next[i]==NULL)
{
fath=fath->fail;
}
if(fath!=NULL)
{
cnt->fail=fath->next[i];
cnt->flag|=cnt->fail->flag;//更新flag 否者下面要一直往上查询
}
else
{
cnt->fail=p;
}
que[rear++]=cnt;
}
}
}
}
char str[maxn];
int len;
int dp[maxn][maxn];
/*
dp[i][j] 表示到目标串的第 i 个字符,DFA 状态为 j 时的最小修改数量。
则 dp[i][ son[j] ]= min{ dp[i][ son[j] ], dp[i-1][j]+ (tran[j][son[j]]
!= str[i])},tran[j][ son[j] ]表示从 j 到 son[j] 经过的字母边。
*/
void calc()
{
len=strlen(str);
for(int i=0;i<=len;i++) for(int j=0;j<rear;j++) dp[i][j]=inf;
dp[0][0]=0;
Node* fath;
for(int i=1;i<=len;i++)
{
for(int j=0;j<rear;j++)
{
Node* p=&temp[j];
if(dp[i-1][j]!=inf&&!p->flag)
{
for(int t=0;t<4;t++)
{
int k;
Node* cnt=p->next[t];
if(cnt!=NULL)
{
if(!cnt->flag)
{
k=cnt->id;
dp[i][k]=min(dp[i][k],dp[i-1][j]+(t!=hash[str[i-1]]));
}
}
else
{
fath=p->fail;
while(fath!=NULL&&fath->next[t]==NULL)
{
fath=fath->fail;
}
if(fath!=NULL)
{
cnt=fath->next[t];
if(!cnt->flag)
{
k=cnt->id;
dp[i][k]=min(dp[i][k],dp[i-1][j]+(t!=hash[str[i-1]]));
}
}
else
{
fath=root;
k=0;
dp[i][k]=min(dp[i][k],dp[i-1][j]+(t!=hash[str[i-1]]));
}
}
}
}
}
}
}
int main()
{
int pl=1;
while(scanf("%d",&n)==1&&n)
{
init();
for(int i=0;i<n;i++)
{
scanf("%s",str);
insert(str);
}
DFA();
scanf("%s",str);
calc();
int ans=inf;
for(int i=0;i<rear;i++) ans=min(ans,dp[len][i]);
printf("Case %d: ", pl++ );
if( ans== inf ) printf("-1\n");
else printf("%d\n", ans );
}
return 0;
}