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): 2612    Accepted Submission(s): 1406


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个模式串,再给一个字符串s,问最少改变s的几个字符,使s不包含这n个模式串。


首先,根据所给字符串构造trie图。

之后,可以DP了。

dp[i][j]表示字符串长度为i,状态为自动机上状态j时的最少改动字符数。

则dp[i][j]=min(dp[i-1][k]),其中j状态可在自动机上由k状态转化而来。

边界条件:dp[0][0]=0, 0为自动机上根的状态。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <queue>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=1005,inf=0x3f3f3f3f,maxk=4;
char s[maxn];
char d[4];
int dp[maxn][maxn];
int id['Z'+1];
int num;

struct node{
	struct node *fail;
	struct node *next[maxk];
	int cnt,id;
	void init() {
		fail=NULL;
		for (int i=0;i<maxk;i++) next[i]=NULL;
		cnt=0;
		id=++num;
	}
};
node *a[maxn],*q[maxn];

void insert(string s,node *root,int len) {
	node *now=root;
	int i;
	for (i=0;i<len;i++) {
		int pos=id[s[i]];
		if (now->next[pos]==NULL) {
	    	now->next[pos]=new node;
	    	now->next[pos]->init();
	    	a[num]=now->next[pos];
    	}
    	now=now->next[pos];
	}
	now->cnt++;
}

void buildfail(node *root) {
	node *p=root;
	int front=0,tail=0,i;
	for (i=0;i<maxk;i++) {
		if (p->next[i]!=NULL) {
			p->next[i]->fail=root;
			q[tail++]=p->next[i];
		} else p->next[i]=root;
	}
	while (front<tail) {
		p=q[front];
		for (i=0;i<maxk;i++) {
			if (p->next[i]==NULL) 
			    p->next[i]=p->fail->next[i];
			else {
				node *x=p->fail;
				while (x!=NULL) {
					if (x->next[i]!=NULL) {
						p->next[i]->fail=x->next[i];
						break;
					}
					x=x->fail;
				}
				if (p->fail->next[i]->cnt) p->next[i]->cnt++;
				q[tail++]=p->next[i];
			}
		}
		front++;
	}
}

int main() {
	int cas=0,n;
	scanf("%d",&n);
	id['A']=0;id['T']=1;id['C']=2;id['G']=3;
	d[0]='A';d[1]='T';d[2]='C';d[3]='G';
	while (n) {
		int i,len,j,k;
		cas++;
		num=0;
		node *root=new node;
		root->init();
		a[1]=root;
		for (i=1;i<=n;i++) {
			scanf("%s",s);
			insert(s,root,strlen(s));
		}
		buildfail(root);
		scanf("%s",s);
		len=strlen(s);
		meminf(dp);
		dp[0][1]=0;
		for (i=1;i<=len;i++) {
			for (j=1;j<=num;j++) {
				if (dp[i-1][j]==inf) continue;
				for (k=0;k<4;k++) {
					if (a[j]->next[k]->cnt==0) {
						dp[i][a[j]->next[k]->id]=
						min(dp[i][a[j]->next[k]->id],
						dp[i-1][j]+(d[k]==s[i-1]?0:1));
					}
				}
			}
		}
		int ans=inf;
		for (i=1;i<=num;i++) 
		    ans=min(ans,dp[len][i]);
		if (ans==inf) ans=-1;
		printf("Case %d: %d\n",cas,ans);
		scanf("%d",&n);
    }
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值