poj 3691 DNA repair(自动机+DP)
来源:互联网 发布:达内 编程配套的 编辑:程序博客网 时间:2024/05/16 06:18
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 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
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
#include<stdio.h>#include<string.h>#define inf 0xffffffstruct trie{ int next[4],fail,flag;}tree[1005];int dp[1005][1005];int que[1005],all;char s[1005];int MIN(int a,int b){ return a<b?a:b;}void init(int x){ memset(tree[x].next,0,sizeof(tree[x].next)); tree[x].fail=0; tree[x].flag=0;}int change(char c){ if(c=='A') return 0; else if(c=='T') return 1; else if(c=='C') return 2; else return 3;}void myinsert(char *str){ int i,temp,now=0; for(i=0;str[i];i++) { temp=change(str[i]); if(!tree[now].next[temp]) { init(++all); tree[now].next[temp]=all; } now=tree[now].next[temp]; } tree[now].flag=1;}void mybuildac(){ int sta=0,fin=0,i,now; for(i=0;i<4;i++) { if(!tree[0].next[i]) continue; que[fin++]=tree[0].next[i]; } while(sta<fin) { now=que[sta++]; for(i=0;i<4;i++) { if(!tree[now].next[i]) tree[now].next[i]=tree[tree[now].fail].next[i]; else { tree[tree[now].next[i]].fail=tree[tree[now].fail].next[i]; if(tree[tree[tree[now].next[i]].fail].flag) tree[tree[now].next[i]].flag=1; que[fin++]=tree[now].next[i]; } } }}int solve(){ int i,j,k,temp,res,len=strlen(s); for(i=0;i<=len;i++) { for(j=0;j<all;j++) dp[i][j]=inf; } dp[0][0]=0; for(i=1;i<=len;i++) { for(j=0;j<all;j++) { if(dp[i-1][j]<inf) { for(k=0;k<4;k++) { if(!tree[tree[j].next[k]].flag) { temp=tree[j].next[k]; dp[i][temp]=MIN(dp[i][temp],dp[i-1][j]+(change(s[i-1])!=k)); } } } } } for(res=inf,i=0;i<all;i++) { if(tree[i].flag==0&&res>dp[len][i]) res=dp[len][i]; } return res<inf?res:-1;}int main(){ int i,n,cas=1; char str[28]; while(scanf("%d",&n)&&n) { init(0); for(all=i=0;i<n;i++) { scanf("%s",str); myinsert(str); } mybuildac(); scanf("%s",s); printf("Case %d: %d\n",cas++,solve()); } return 0;}
- poj 3691 DNA repair(自动机+DP)
- POJ--3691[DNA repair] AC自动机+DP
- POJ 3691:DNA repair(AC自动机+DP)
- POJ 3691 DNA repair【AC自动机+DP】
- POJ 3691:DNA repair【AC自动机+DP】
- POJ 3691 DNA repair AC自动机DP
- poj 3691 DNA repair AC自动机+dp
- POJ 3691 DNA repair (AC自动机+dp)
- POJ 3691 DNA repair(自动机+DP)
- poj 3691 DNA repair(AC自动机+DP)
- POJ 3691 DNA repair (AC自动机 + dp)
- poj 3691 DNA repair AC自动机+DP
- POJ 3691 DNA repair AC自动机+DP -
- poj 3691 DNA repair AC自动机 dp
- POJ 3691 DNA repair AC自动机 + dp
- poj 3691 DNA repair(AC自动机+dp)
- POJ 3691 DNA repair(AC自动机+dp)
- HDU 2457 || POJ 3691 DNA repair (AC自动机 + dp)
- 4.12 基于OHCI的USB主机 —— USB设备设置地址
- 小花真封神之天尊地魔游戏引擎技术交流论坛正式开通www.zfs2014.com
- 算法练笔
- Viojs P1001 谁拿了最多的奖学金
- Python 实现ping工具。可配置多个ip
- poj 3691 DNA repair(自动机+DP)
- HDU 3572 网络流
- 变压器的绕制
- 解决问题--VS2012中一个Panel覆盖另一个Panel时拖动时容易造成两个控件成父子关系的避免
- HTTPS post: bypass SSL security check and trust all the hosts
- 异步设备I/O--请求加入队列与接受完成通知
- 排序算法之折半插入算法
- 限制textView的输入字符长度
- Java JMF 多媒体框架