hdu2457:DNA repair

AC自动机+dp。问改变多少个字符能让目标串不含病毒串。即走过多少步不经过病毒串终点。又是同样的问题。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define REP(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
const int nmax=1005;
const int inf=0x7f7f7f7f;
int ch[nmax][4],fail[nmax],pt=0,dp[nmax][nmax];
bool F[nmax];
char s[nmax];
int id(char c){
	if(c=='A') return 0;
	if(c=='C') return 1;
	if(c=='G') return 2;
	else return 3;
}
void insert(){
	int t=0,len=strlen(s);
	REP(i,0,len-1) {
		if(!ch[t][id(s[i])]) ch[t][id(s[i])]=++pt;
		t=ch[t][id(s[i])];
	}
	F[t]=true;
}
queue<int>q;
void getfail(){
	q.push(0);fail[0]=0;
	while(!q.empty()){
		int x=q.front();q.pop();
		REP(i,0,3){
			if(ch[x][i]) q.push(ch[x][i]),fail[ch[x][i]]=x==0?0:ch[fail[x]][i];
			else ch[x][i]=x==0?0:ch[fail[x]][i];
		}
		F[x]|=F[fail[x]];
	}
}
void work(int x){
	clr(dp,0x7f);dp[0][0]=0;
	scanf("%s",s);int len=strlen(s);
	REP(i,0,len-1) REP(j,0,pt) if(dp[i][j]!=inf){
		REP(k,0,3){
			int tmp=ch[j][k];if(F[tmp]) continue;
			int temp=(k==id(s[i]))?dp[i][j]:dp[i][j]+1;
			dp[i+1][tmp]=min(dp[i+1][tmp],temp);
		}
	}
	int ans=inf;
	REP(i,0,pt) ans=min(ans,dp[len][i]);
	if(ans==inf) printf("Case %d: -1\n",x);
	else printf("Case %d: %d\n",x,ans);
}
int main(){
	int n,cur=0;
	while(scanf("%d",&n)!=EOF&&n){
		clr(fail,0);clr(ch,0);clr(F,false);pt=0;
		REP(i,1,n) scanf("%s",s),insert();
		getfail();work(++cur);
	}
	return 0;
}

  


DNA repair

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


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
 

Recommend
teddy | We have carefully selected several similar problems for you: 2458 2459 2456 2461 2462

转载于:https://www.cnblogs.com/fighting-to-the-end/p/5712845.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值