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 repair

Time
Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K
(Java/Others)
Total Submission(s): 2204 Accepted Submission(s):
1168

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.

 
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.

 
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
teddy | We have carefully selected several similar
problems for you: 2458 2459 2456 2461 2462

hdu2457:DNA repair的更多相关文章

  1. HDU2457 DNA repair —— AC自动机 + DP

    题目链接:https://vjudge.net/problem/HDU-2457 DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory ...

  2. HDU2457 DNA repair(AC自动机+DP)

    题目一串DNA最少需要修改几个基因使其不包含一些致病DNA片段. 这道题应该是AC自动机+DP的入门题了,有POJ2778基础不难写出来. dp[i][j]表示原DNA前i位(在AC自动机上转移i步) ...

  3. [hdu2457]DNA repair(AC自动机+dp)

    题意:给出一些不合法的模式DNA串,给出一个原串,问最少需要修改多少个字符,使得原串中不包含非法串. 解题关键:多模式串匹配->AC自动机,求最优值->dp,注意在AC自动机上dp的套路. ...

  4. HDU 2425 DNA repair (AC自动机+DP)

    DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. 【POJ3691】 DNA repair (AC自动机+DP)

    DNA repair Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description B ...

  6. POJ 3691 &amp; 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: ...

  7. POJ 3691 DNA repair (DP+AC自动机)

    DNA repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4815   Accepted: 2237 Descri ...

  8. HDU 2457 DNA repair(AC自动机+DP)题解

    题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...

  9. poj 3691 DNA repair(AC自己主动机+dp)

    DNA repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5877   Accepted: 2760 Descri ...

随机推荐

  1. 历时一周,unity3d+xtion打造我的第一个休闲体感小游戏《空降奇兵》

    1.游戏介绍 本游戏属于休闲小游戏,主要操作如下: 菜单控制:举起左手或右手,点击左边或者右边的菜单:挥动左手或右手,选择关卡: 操作方式:玩家跳跃,游戏中的伞兵从飞机开始降落:玩家通过控制伞兵的左右 ...

  2. 阿里云之OSS 开放存储服务开发笔记

    在使用云服务以后,你不用考虑他是否能承受压力,而是费用.不要考虑是否被攻击,而是他的API实现.本人开发阿里云服务也走了些崎岖之路,写下以备忘之. 阿里云的开放存储服务可以提供文件的存储服务,开放了上 ...

  3. Atmel Studio 6.0新建项目

        使用Atmel Studio 6.0新建一个项目 连接Atmel 开发板与调试板,开发板使用的芯片是ATXMGEA128A1 注意: 以上对于开发板与调试板的连接,需要非常注意连接时线的方向, ...

  4. HDAO

    dx11 hdao10.1 除了dx的sample竟然搜不到什么文档.... 估计去问别人也是让我继续看代码.. ---------------------------------------- 算法 ...

  5. iOS 沙盒购买,弹出“需要验证”,“继续登录”的问题?

    点击购买后,能弹出 确认购买的对话框, 您想以xxx的价格买一个xxx吗? [environment:sandbox] 点击确认购买后,弹出"需要验证" 点击继续,输入密码后.竟然 ...

  6. 别让安全问题拖慢了 DevOps!

    DEVSECOPS 所面临的挑战 敏捷开发和 DevOps 方法的出现使软件开发的速度与质量都有所提升,但它们不经意地也为安全机构增压不少.从前的安全策略是基于静态数据的,而在产品上线前才应用这些策略 ...

  7. git安装及使用

    一.安装 1.从http://code.google.com/p/msysgit/下载Git-1.8.4-preview20130916.exe,并安装. 2.新建git目录,右键选择Git Bash ...

  8. PHP Zend Studio9.0怎么把代码搞成和服务器端的同步(就是直接在服务器端修改)

    Zend Studio 可以直接通过Remote System的方式直接连接服务器端的代码,就是可以直接修改服务器端的代码,不过修改的时间小心点,修改就会立即生效的. 选择Remote Systems ...

  9. 目标检测的图像特征提取之(三)Haar特征

    1.Haar-like特征 Haar-like特征最早是由Papageorgiou等应用于人脸表示,Viola和Jones在此基础上,使用3种类型4种形式的特征. Haar特征分为三类:边缘特征.线性 ...

  10. css元素z-index设置为什么不起作用?

    元素位置重叠的背景常识 (x)html文档中的元素默认处于普通流(normal flow)中,也就是说其顺序由元素在文档中的先后位置决定,此时一般不会产生重叠(但指定负边距可能产生重叠). 当我们用c ...