hdu2457:DNA repair
AC自动机+dp。问改变多少个字符能让目标串不含病毒串。即走过多少步不经过病毒串终点。又是同样的问题。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define REP(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
const int nmax=1005;
const int inf=0x7f7f7f7f;
int ch[nmax][4],fail[nmax],pt=0,dp[nmax][nmax];
bool F[nmax];
char s[nmax];
int id(char c){
if(c=='A') return 0;
if(c=='C') return 1;
if(c=='G') return 2;
else return 3;
}
void insert(){
int t=0,len=strlen(s);
REP(i,0,len-1) {
if(!ch[t][id(s[i])]) ch[t][id(s[i])]=++pt;
t=ch[t][id(s[i])];
}
F[t]=true;
}
queue<int>q;
void getfail(){
q.push(0);fail[0]=0;
while(!q.empty()){
int x=q.front();q.pop();
REP(i,0,3){
if(ch[x][i]) q.push(ch[x][i]),fail[ch[x][i]]=x==0?0:ch[fail[x]][i];
else ch[x][i]=x==0?0:ch[fail[x]][i];
}
F[x]|=F[fail[x]];
}
}
void work(int x){
clr(dp,0x7f);dp[0][0]=0;
scanf("%s",s);int len=strlen(s);
REP(i,0,len-1) REP(j,0,pt) if(dp[i][j]!=inf){
REP(k,0,3){
int tmp=ch[j][k];if(F[tmp]) continue;
int temp=(k==id(s[i]))?dp[i][j]:dp[i][j]+1;
dp[i+1][tmp]=min(dp[i+1][tmp],temp);
}
}
int ans=inf;
REP(i,0,pt) ans=min(ans,dp[len][i]);
if(ans==inf) printf("Case %d: -1\n",x);
else printf("Case %d: %d\n",x,ans);
}
int main(){
int n,cur=0;
while(scanf("%d",&n)!=EOF&&n){
clr(fail,0);clr(ch,0);clr(F,false);pt=0;
REP(i,1,n) scanf("%s",s),insert();
getfail();work(++cur);
}
return 0;
}
|
DNA repairTime 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 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 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 Source
Recommend
|
hdu2457:DNA repair的更多相关文章
- HDU2457 DNA repair —— AC自动机 + DP
题目链接:https://vjudge.net/problem/HDU-2457 DNA repair Time Limit: 5000/2000 MS (Java/Others) Memory ...
- HDU2457 DNA repair(AC自动机+DP)
题目一串DNA最少需要修改几个基因使其不包含一些致病DNA片段. 这道题应该是AC自动机+DP的入门题了,有POJ2778基础不难写出来. dp[i][j]表示原DNA前i位(在AC自动机上转移i步) ...
- [hdu2457]DNA repair(AC自动机+dp)
题意:给出一些不合法的模式DNA串,给出一个原串,问最少需要修改多少个字符,使得原串中不包含非法串. 解题关键:多模式串匹配->AC自动机,求最优值->dp,注意在AC自动机上dp的套路. ...
- HDU 2425 DNA repair (AC自动机+DP)
DNA repair Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 【POJ3691】 DNA repair (AC自动机+DP)
DNA repair Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description B ...
- POJ 3691 & HDU 2457 DNA repair (AC自己主动机,DP)
http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...
- POJ 3691 DNA repair (DP+AC自动机)
DNA repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4815 Accepted: 2237 Descri ...
- HDU 2457 DNA repair(AC自动机+DP)题解
题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...
- poj 3691 DNA repair(AC自己主动机+dp)
DNA repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5877 Accepted: 2760 Descri ...
随机推荐
- Emmet快速编写CSS样式
基本的CSS样式编写时,很多样式只需输入首字母即可得到不带属性值的CSS样式,像上面说到的margin. 1.而对于一些带有特定的属性值的CSS样式,只需输入CSS标签与属性值的首字母就可以,比如: ...
- make -f dc_debug.mak 提示错误"/usr/bin/ld:can not find -l***"解决办法
在公司不同服务器上"make -f ***"程序的时候,有的服务器可以编译通过,有的却提示"/usr/bin/ld:can not find -l***"的错误 ...
- 盘点 DevOps 世界的杰出女性(一)
[编者按]IT 领域从来不缺乏杰出的女性存在,近日,DevOps.com 主编 Alan Shimel 盘点了 DevOps 领域的杰出女性,首期为六个,本文系 OneAPM 工程师编译整理. 以下为 ...
- EF提供的三种查询方式
這邊簡單介紹一下,ADO.Net Entity Framework 提供的三種查詢方式, Linq to Entities Query Builder Mothed Entity SQL Langua ...
- 【redis】03list类型
list类型 redis的list类型是一个链表结构,他的主要功能是push.pop.获取一个范围的所有值等等一些操作, 咱们push什么意思,push是不是相当于咱们php里面的array_push ...
- [转载]Spring Bean Configuration Inheritance
转自: http://www.mkyong.com/spring/spring-bean-configuration-inheritance/ In Spring, the inheritance i ...
- HDU3068 最长回文 Manacher算法
Manacher算法是O(n)求最长回文子串的算法,其原理很多别的博客都有介绍,代码用的是clj模板里的,写的确实是异常的简洁,现在的我只能理解个大概,下面这个网址的介绍比较接近于这个模板,以后再好好 ...
- crontab定时运行git命令 更新代码库
Q: http://stackoverflow.com/questions/7994663/git-push-via-cron I'm trying to run a git push fro ...
- https 方式使用git@osc设置密码的方式
https方式每次都要输入密码,按照如下设置即可输入一次就不用再手输入密码的困扰而且又享受https带来的极速 设置记住密码(默认15分钟): git config --global credenti ...
- hdu 4676 Sum Of Gcd
离线+分块!! 思路:序列a[1],a[2],a[3]……a[n] num[i]表示区间[L,R]中是i的倍数的个数:euler[i]表示i的欧拉函数值. 则区间的GCD之和sum=∑(C(num[i ...