hdu2457 DNA repair(AC自动机+dp)
来源:互联网 发布:mac系统序列号查询 编辑:程序博客网 时间:2024/06/04 18:20
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1930 Accepted Submission(s): 1046
Total Submission(s): 1930 Accepted Submission(s): 1046
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.
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.
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.
number of characters which need to be changed. If it's impossible to repair the given DNA, print -1.
Sample Input
2AAAAAGAAAG 2ATGTGAATG4AGCTAGT0
Sample Output
Case 1: 1Case 2: 4Case 3: -1
题意:给你n个模板串和一个字符串,让你对字符串里的字符进行修改,使得修改后不含模板串中的任何一个,求最小的修改次数。
思路:先构造trie图,其实trie图里的next数组就是状态转移过程了,我们只要定义dp[i][j]表示长度为i,到节点j所要修改的最少次数。那么if(t==idx(s[i]) )dp[i+1][jj ]=min(dp[i+1][jj],dp[i][j] );else{dp[i+1][jj]=min(dp[i+1 ][jj],dp[i][j]+1 );}
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<string>#include<algorithm>using namespace std;typedef long long ll;#define inf 99999999#define pi acos(-1.0)#define maxnode 1200int dp[maxnode][maxnode];char s[55],str[1006];int tot=0;struct trie{ int sz,root,val[maxnode],next[maxnode][6],fail[maxnode]; int q[1111111]; void init(){ int i; sz=root=0; val[0]=0; for(i=0;i<4;i++){ next[root][i]=-1; } } int idx(char c){ if(c=='A')return 0; if(c=='G')return 1; if(c=='C')return 2; if(c=='T')return 3; } void charu(char *s){ int i,j,u=0; int len=strlen(s); for(i=0;i<len;i++){ int c=idx(s[i]); if(next[u][c]==-1){ sz++; val[sz]=0; next[u][c]=sz; u=next[u][c]; for(j=0;j<4;j++){ next[u][j]=-1; } } else{ u=next[u][c]; } } val[u]=1; } void build(){ int i,j; int front,rear; front=1;rear=0; for(i=0;i<4;i++){ if(next[root][i]==-1 ){ next[root][i]=root; } else{ fail[next[root][i] ]=root; rear++; q[rear]=next[root][i]; } } while(front<=rear){ int x=q[front]; if(val[fail[x] ])val[x]=1; front++; for(i=0;i<4;i++){ if(next[x][i]==-1){ next[x][i]=next[fail[x] ][i]; } else{ fail[next[x][i] ]=next[fail[x] ][i]; rear++; q[rear]=next[x][i]; } } } } void solve(char *s) { int i,j,len,t,jj; len=strlen(s); for(i=0;i<=len;i++){ for(j=0;j<=sz;j++){ dp[i][j]=inf; } } dp[0][0]=0; for(i=0;i<=len-1;i++){ for(j=0;j<=sz;j++){ if(dp[i][j]==inf)continue; for(t=0;t<4;t++){ jj=next[j][t]; if(val[jj ])continue; if(t==idx(s[i]) )dp[i+1][jj ]=min(dp[i+1][jj],dp[i][j] ); else{ dp[i+1][jj]=min(dp[i+1 ][jj],dp[i][j]+1 ); } } } } int minx=inf; for(j=0;j<=sz;j++){ if(val[j])continue; minx=min(minx,dp[len][j]); } printf("Case %d: ",++tot); if(minx>1000000)printf("-1\n"); else printf("%d\n",minx); }}ac;int main(){ int n,m,i,j; while(scanf("%d",&n)!=EOF && n!=0) { ac.init(); for(i=1;i<=n;i++){ scanf("%s",s); ac.charu(s); } ac.build(); scanf("%s",str); ac.solve(str); } return 0;}
0 0
- hdu2457 & poj3691 DNA repair AC自动机+DP
- hdu2457 DNA repair AC自动机+DP
- hdu2457---DNA repair(AC自动机+dp)
- HDU2457 DNA repair AC自动机+DP
- hdu2457 DNA repair(AC自动机+dp)
- hdu2457 DNA repair(AC自动机+dp)
- hdu2457 poj3691 DNA Repair AC自动机 + dp
- HDU2457 DNA repair AC自动机
- [HDU2457]DNA repair-AC自动机
- HDU2457 DNA repair AC自动机上的简单DP
- hdu2457 DNA repair ac机+dp
- hdu2457 AC自动机+DP
- [AC自动机+dp]PKU 3691--DNA repair
- POJ--3691[DNA repair] AC自动机+DP
- POJ 3691:DNA repair(AC自动机+DP)
- 【AC自动机+DP】 hdu3691 DNA repair
- POJ 3691 DNA repair【AC自动机+DP】
- hdu 2457 DNA repair(AC自动机+DP)
- 87
- nginx高级配置
- hdoj 2052 picture
- 110. Balanced Binary Tree LeetCode
- HDU3488 Cyclic Tour(有向环最小覆盖)
- hdu2457 DNA repair(AC自动机+dp)
- linux 安装 percona-toolkit
- neuq oj 1017平面切割(特别版)C++
- firewalld自定义服务名及端口
- 101. Symmetric Tree LeetCode
- webroot和web-inf的区别
- Ubuntu配置
- 欢迎使用CSDN-markdown编辑器
- 图片画画板