hdu2457 DNA repair ac机+dp
来源:互联网 发布:少儿编程 童程童美 编辑:程序博客网 时间:2024/05/27 20:55
DNA repair
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1201 Accepted Submission(s): 649
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
Source
2008 Asia Hefei Regional Contest Online by USTC
Recommend
teddy | We have carefully selected several similar problems for you: 2458 2459 2456 2461 2462
题意:给定n个仅由A、G、C、T构成的非法串,又给定一个仅由A、G、C、T构成的字符串s,问最少需要多少步使得s不包含非法串,如若不能输出-1。
思路:先将A、C、G、T编号,并将n个非法串建ac自动机,设dp[i][j]表示长度为i的串以ac上j结点为末尾所需修改的最少步数,设p=trie[j].next[k]->id, 那么dp[i+1][p]=min(dp[i][j]+(k!=idx(str[i])),dp[i+1][p]),转移的前提是p,j结点必须均为合法结点,即该结点的cnt不为1。详见代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int sigma_size=4;const int MAXN=1000+100;const int inf=0x3fffffff;int n,head,tail,sz;int dp[MAXN][MAXN];char s[MAXN];struct node{ int cnt,id; node *next[sigma_size],*fail;}trie[MAXN],*root,*que[MAXN];struct AC{ node *createnode() { for(int k=0;k<sigma_size;k++) trie[sz].next[k]=NULL; trie[sz].fail=NULL; trie[sz].cnt=0,trie[sz].id=sz; return &trie[sz++]; } void init() { sz=0; head=tail=0; root=createnode(); } int idx(char c) { if(c=='A') return 0; if(c=='G') return 1; if(c=='C') return 2; return 3; } void insert(char *str) { node *p=root; int len=strlen(str); for(int i=0;i<len;i++) { int k=idx(str[i]); if(p->next[k]==NULL) p->next[k]=createnode(); p=p->next[k]; } p->cnt=1; } void get_fail() { que[tail++]=root; while(head<tail) { node *p=que[head++]; for(int k=0;k<sigma_size;k++) if(p->next[k]) { if(p==root) p->next[k]->fail=root; else p->next[k]->fail=p->fail->next[k]; if(p->next[k]->fail->cnt) p->next[k]->cnt=1; que[tail++]=p->next[k]; } else { if(p==root) p->next[k]=root; else p->next[k]=p->fail->next[k]; } } } int query(char *str) { int len=strlen(str); for(int i=0;i<=len;i++) for(int j=0;j<sz;j++) dp[i][j]=inf; dp[0][0]=0; for(int i=0;i<len;i++) for(int j=0;j<sz;j++) { if(trie[j].cnt) continue; for(int k=0;k<sigma_size;k++) { int p=trie[j].next[k]->id; if(trie[p].cnt) continue; dp[i+1][p]=min(dp[i+1][p],dp[i][j]+(k!=idx(str[i]))); } } int ans=inf; for(int i=0;i<sz;i++) ans=min(ans,dp[len][i]); if(ans==inf) ans=-1; return ans; }}ac;int main(){ freopen("text.txt","r",stdin); int kase=0; while(~scanf("%d",&n)&& n) { kase++; ac.init(); for(int i=0;i<n;i++) { scanf("%s",s); ac.insert(s); } ac.get_fail(); scanf("%s",s); printf("Case %d: %d\n",kase,ac.query(s)); } return 0;}
0 0
- hdu2457 DNA repair ac机+dp
- 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
- DNA repair hdu2457
- HDU2457--DNA repair
- HDU2457 DNA repair
- HDU2457-DNA repair
- [AC自动机+dp]PKU 3691--DNA repair
- POJ--3691[DNA repair] AC自动机+DP
- TCP 的那些事儿(上)
- 溢出
- OpenCV基础数据结构--基础入门
- 使用google自带包实现下拉刷新功能
- 超级英雄伙伴介绍
- hdu2457 DNA repair ac机+dp
- 四种途径将HTML5 web应用变成android应用
- Banana pi 香蕉派网站正式上线 www.banana-pi.com
- IOS应用程序多语言本地化解决方案
- 极通EWEBS医疗系统信息化集成解决方案
- 我在接入安卓SDK时,关于Activity继承Cocos2dxActivity onkeydown无法监测的问题
- 昔年——落忆
- 如何选择地板颜色 搭配客厅与卧室风水
- 数据库碎片学习:独占锁、共享锁、更新锁,乐观锁、悲观锁