[poj3691] DNA repair
来源:互联网 发布:centos 打不了中文 编辑:程序博客网 时间:2024/04/28 06:56
题目描述
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.
输入格式
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.
输出格式
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.
样例数据
样例输入
2
AAA
AAG
AAAG
2
A
TG
TGAATG
4
A
G
C
T
AGT
0
样例输出
Case 1: 1
Case 2: 4
Case 3: -1
题目分析
和poj1625差不多,就不细讲了。
建立AC自动机方法一样,动规思路一样,不需要高精度,不需要调试,1A棒棒的
源代码
#include<algorithm>#include<iostream>#include<iomanip>#include<cstring>#include<cstdlib>#include<vector>#include<cstdio>#include<cmath>#include<queue>using namespace std;inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj;}const int maxn=1005;struct Tree { int child[15],fail,flag; //fail失败指针 void clear() { memset(child,0,sizeof(child)); fail=0; flag=0; }};int root=1,n,Map[1005];struct Aho_Corasick_Automaton { //AC自动机 int cnt; Tree tree[maxn]; void init() { cnt=1; memset(tree,0,sizeof(tree)); } void insert(string s) { int now=root,len=s.length(); for(int i=0; i<len; i++) { int j=Map[s[i]]; if(!tree[now].child[j]) { tree[++cnt].clear(); tree[now].child[j]=cnt; } now=tree[now].child[j]; } tree[now].flag=1; } void buildfail() { //Bfs构造Fail指针 queue<int>Q; Q.push(root); while(!Q.empty()) { int Now=Q.front(); Q.pop(); for(int i=0; i<4; i++) { int Next=tree[Now].child[i]; if(Next==0) { //儿子不存在 if(tree[tree[Now].fail].child[i])tree[Now].child[i]=tree[tree[Now].fail].child[i]; else tree[Now].child[i]=root; continue; } Q.push(Next); int fatherfail=tree[Now].fail; //父亲的失败指针 while(fatherfail&&!tree[fatherfail].child[i])fatherfail=tree[fatherfail].fail; //寻找可退回点 if(fatherfail)tree[Next].fail=tree[fatherfail].child[i]; //如果存在满足条件的点则设置失败指针 else tree[Next].fail=root; //否则指回root tree[Next].flag|=tree[tree[Next].fail].flag; } } }};Aho_Corasick_Automaton ac;long long f[1005][1005],ans=0x7fffffff/2,tot=0;string DNA;int main() { ios::sync_with_stdio(false); Map['A']=0; Map['G']=1; Map['C']=2; Map['T']=3; while(true) { cin>>n; if(n==0)break; ans=0x7fffffff/2; ac.init(); for(int i=1; i<=n; i++) { string s; cin>>s; ac.insert(s); } cin>>DNA; int len=DNA.length(); ac.buildfail(); for(int i=0; i<=len; i++) for(int j=1; j<=ac.cnt; j++) f[i][j]=0x7fffffff/2; f[0][1]=0; for(int i=1; i<=len; i++) for(int j=1; j<=ac.cnt; j++) { if(ac.tree[j].flag)continue; for(int k=0; k<4; k++) { if(ac.tree[ac.tree[j].child[k]].flag)continue; f[i][ac.tree[j].child[k]]=min(f[i][ac.tree[j].child[k]],f[i-1][j]+(Map[DNA[i-1]]!=k)); } } printf("Case %d: ",++tot); for(int i=1; i<=ac.cnt; i++)ans=min(ans,f[len][i]); if(ans!=0x7fffffff/2)printf("%lld\n",ans); else puts("-1"); } return 0;}
- poj3691 DNA repair
- poj3691 DNA repair
- [poj3691] DNA repair
- POJ3691 DNA repair 题解&代码
- POJ3691 DNA repair AC自动机+动态规划
- hdu2457 & poj3691 DNA repair AC自动机+DP
- 【POJ3691】DNA repair AC自动机+DP
- poj3691--DNA repair(AC自动机+dp)
- poj3691 DNA repair dp AC自动机
- POJ3691.DNA repair (Trie图 && DP)
- hdu2457 poj3691 DNA Repair AC自动机 + dp
- 【ac自动机·DP】poj3691 DNA Repair
- DNA repair
- poj 3691 DNA repair
- hdu2457 DNA repair
- DNA repair hdu2457
- [AC自动机]DNA Repair
- HDU2457--DNA repair
- 截取Chrome下载的mp3
- Spring AOP
- SQLite自定义聚合函数 [类似MAX(field)]
- 派生类虚函数调用基类版本
- Android onCreate 详解
- [poj3691] DNA repair
- android如何实现特效
- Android MVP 实例
- 晃晃悠悠的2016,我不愿提、不愿想;简单思维的我,心将该去何方
- 项目版本号
- 【Linux/安卓+SPI以太网项目】
- 【IO】程序访问文件的几种方式
- Linux下安装jdk1.8
- 无法在只读列“id”中插入数据