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

本文介绍了一种使用AC自动机解决修复DNA序列问题的方法。通过构建自动机并利用动态规划,实现对DNA序列的高效修复,以消除特定的疾病引发片段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DNA repair

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2640    Accepted Submission(s): 1419


Problem 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 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.
 

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

题意:给你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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值