hdu 2457 ac自动机+简单dp

DNA repair

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


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 求不含病毒串所需要的最少修改。简单dp 代码:
/* ***********************************************
Author :xianxingwuguan
Created Time :2014-2-3 14:41:38
File Name :1.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
int dp[1010][1010];
struct Trie{
	int next[1010][4],end[1010],fail[1010];
	int root,L;
	int newnode(){
		for(int i=0;i<4;i++)
			next[L][i]=-1;
		end[L++]=0;
		return L-1;
	}
	void init(){
		L=0;
		root=newnode();
	}
	int id(char ch){
		if(ch==‘A‘)return 0;
		if(ch==‘T‘)return 1;
		if(ch==‘C‘)return 2;
		return 3;
	}
	void insert(char *str){
		int len=strlen(str);
		int now=root;
		for(int i=0;i<len;i++){
			int p=id(str[i]);
			if(next[now][p]==-1)
				next[now][p]=newnode();
			now=next[now][p];
		}
		end[now]=1;
	}
	void build(){
		queue<int> q;
		fail[root]=root;
		for(int i=0;i<4;i++)
			if(next[root][i]==-1)
				next[root][i]=root;
			else {
				fail[next[root][i]]=root;
				q.push(next[root][i]);
			}
		while(!q.empty()){
			int now=q.front();
			q.pop();
			end[now]|=end[fail[now]];
			for(int i=0;i<4;i++)
				if(next[now][i]==-1)
					next[now][i]=next[fail[now]][i];
				else {
					fail[next[now][i]]=next[fail[now]][i];
					q.push(next[now][i]);
				}
		}
	}
	int solve(char *str){
		int n=strlen(str);
		for(int i=0;i<=n;i++)
			for(int j=0;j<L;j++)
				dp[i][j]=INF;
		dp[0][root]=0;
		for(int i=0;i<n;i++)
			for(int j=0;j<L;j++)
				if(dp[i][j]<INF){
					for(int k=0;k<4;k++){
						int p=next[j][k];
						if(end[p])continue;
						int tt=dp[i][j]+(id(str[i])!=k);
						dp[i+1][p]=min(dp[i+1][p],tt);
					}
				}
		int ans=INF;
		for(int i=0;i<L;i++)
			ans=min(ans,dp[n][i]);
		if(ans==INF)ans=-1;
		return ans;
	}
}ac;
char str[1010];
int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
 	 int i,j,k,m,n,T=0;
	 while(~scanf("%d",&n)&&n){
		 ac.init();
		 while(n--){
			 scanf("%s",str);
			 ac.insert(str);
		 }
		 ac.build();
		 //cout<<"hahhah:"<<ac.L<<endl;
		 scanf("%s",str);
		 printf("Case %d: %d\n",++T,ac.solve(str));
	 }
     return 0;
}


hdu 2457 ac自动机+简单dp

上一篇:Sql Servicer 复习笔记(1) 存储过程分布


下一篇:[UVA 10557] XYZZY