您的位置:首页 > 大数据 > 人工智能

HDU 2457-DNA repair(AC自动机+DP)

2017-08-09 16:16 429 查看

DNA repair

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2640    Accepted Submission(s): 1419


[align=left]Problem Description[/align]
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.
 

[align=left]Input[/align]
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.
 

[align=left]Output[/align]
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.
 

[align=left]Sample Input[/align]

2
AAA
AAG
AAAG
2
A
TG
TGAATG
4
A
G
C
T
AGT
0

 

[align=left]Sample Output[/align]

Case 1: 1
Case 2: 4
Case 3: -1

 

Source

题意:给你n个病毒序列,再给你个主序列,让你改尽可能少的基因,使得该主序列不含病毒序列

题解:算是AC自动机模板题了,然而我搞了将近5个小时。。。。怪自己太菜,对失败指针这一块还是不太熟练,广搜的时候搞错了,其他还好,将所有病毒序列搞入树中,然后让主串和他们进行匹配,也不算是匹配。既然是不让主序列中含有病毒系列,呢就要将每一个病毒序列的结尾处标记一下,然后DP一下就OK了。。。。

dp[i][j]:前i位满足条件且第i位匹配到自动机的j节点的最小修改次数,下一次的状态很好转移,枚举下一位要改成什么,然后若主序列的第i+1位正好等于当前枚举的基因,则和之前相等即可,否则+1....具体看代码

#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 1000000000
#define mod 100000000
#define maxn 1550
#define lowbit(x) (x&-x)
#define eps 1e-10
int pre[maxn],a[maxn][5],flag[maxn],size,n,ans[maxn],cnt,dp[maxn][maxn],cases;
char s1[maxn][25],s2[2017];
queue<int>q;
int c(char x)
{
if(x=='A')return 0;
if(x=='T')return 1;
if(x=='G')return 2;
if(x=='C')return 3;
}
void insert(int num)
{
int i,len=strlen(s1[num]),now=0,cnt=0;
for(i=0;i<len;i++)
{
int v=c(s1[num][i]);
if(!a[now][v])
{
flag[size]=0;
memset(a[size],0,sizeof(a[size]));
a[now][v]=size++;
}
now=a[now][v];
}
flag[now]=1;
}
void build_fail()
{
int now,i;
for(i=0;i<4;i++)
{
int tmp=a[0][i];
if(tmp)
pre[tmp]=0,q.push(tmp);
}
while(q.empty()==0)
{
now=q.front();
q.pop();
if(flag[pre[now]])
flag[now]=1;
for(i=0;i<4;i++)
{
if(a[now][i]==0)
{
a[now][i]=a[pre[now]][i];
continue;
}
pre[a[now][i]]=a[pre[now]][i];
q.push(a[now][i]);
}
}
}
void match(int num)
{
int i,j,k,len=strlen(s2),ans=inf;
dp[0][0]=0;
for(i=0;i<len;i++)
{
for(j=0;j<size;j++)
{
if(dp[i][j]==inf)
continue;
for(k=0;k<4;k++)
{
int now=a[j][k];
if(flag[now])
continue;
int tmp;
if(k==c(s2[i]))tmp=dp[i][j];
else tmp=dp[i][j]+1;
dp[i+1][now]=min(dp[i+1][now],tmp);
//printf("%d %d %d %d\n",dp[i+1][now],i+1,now,k);
}
}
}
for(i=0;i<size;i++)
ans=min(ans,dp[len][i]);
if(ans==inf)ans=-1;
printf("Case %d: %d\n",++cases,ans);
}
int main(void)
{
int i,j;
while(scanf("%d",&n),n!=0)
{
for(i=0;i<=maxn;i++)
for(j=0;j<=maxn;j++)
dp[i][j]=inf;
size=1;pre[0]=0;flag[0]=0;
memset(a[0],0,sizeof(a[0]));
memset(pre,0,sizeof(pre));
memset(ans,0,sizeof(ans));
for(i=1;i<=n;i++)
{
scanf("%s",s1[i]);
insert(i);
}
build_fail();
scanf("%s",s2);
match(i);
while(q.empty()==0)
q.pop();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: