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

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;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值