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 ...
随机推荐
- C++实现简单的内存池
多进程编程多用在并发服务器的编写上,当收到一个请求时,服务器新建一个进程处理请求,同时继续监听.为了提高响应速度,服务器采用进程池的方法,在初始化阶段创建一个进程池,池中有许多预创建的进程,当请求到达 ...
- Codeforces Round #360 (Div. 2) D. Remainders Game 中国剩余定理
题目链接: 题目 D. Remainders Game time limit per test 1 second memory limit per test 256 megabytes 问题描述 To ...
- 【BZOJ】【1076】【SCOI2008】奖励关
状压DP+数学期望 蒟蒻不会啊……看题跑…… Orz了一下Hzwer,发现自己现在真是太水了,难道不看题解就一道题也不会捉了吗? 题目数据范围不大……100*(2^16)很容易就跑过去了…… DP的时 ...
- eclipse svn 修改了类名之后提交
win下面的文件名不区分大小写,所以不能只是把小写类名改成大写. 正确的做法有如下两种:1,先删除类a,提交,此操作会删除服务器上的文件.再添加类A,提交.2,重命名a为aa,提交,此操作会删除服务器 ...
- PHP之mysql_real_escape_string()函数讲解
定义和用法 mysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的特殊字符. 下列字符受影响: \x00 \n \r \ ' " \x1a 如果成功, ...
- jquery-validation 学习总结
一.用前必备 项目主页:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: http://jquery.bassis ...
- DiskGenius的 “终止位置参数溢出”错误解决方法。
(转帖)同事电脑系统启动突然明显变慢,重装系统后问题仍未解决(windowsxp sp3).帮忙分析感觉是磁盘分区表出现了错误,用通用PE工具箱进入PE系统,DiskGenius工具检查:“终止位置参 ...
- POJ 1781
#include <iostream> #include <string> #include <cmath> using namespace std; unsign ...
- hdu 1002 A+B
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1002 复习一下大数 模板: #include <stdio.h> #include <s ...
- mysql建表时拆分出常用字段和不常用字段
一对一 一张表的一条记录一定只能与另外一张表的一条记录进行对应,反之亦然. 学生表:姓名,性别,年龄,身高,体重,籍贯,家庭住址,紧急联系人 其中姓名.性别.年龄.身高,体重属于常用数据,但是籍贯.住 ...