HDU 2457 DNA repair (AC自动机+DP)
题意:给N个串,一个大串,要求在最小的改变代价下,得到一个不含上述n个串的大串。
思路:dp,f[i][j]代表大串中第i位,AC自动机上第j位的最小代价。
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<queue>
std::queue<int>Q;
int ch[][],end[],f[][],sz,root,fail[],n,Tcase;
char s[];
int newnode(){
for (int i=;i<=;i++)
ch[sz][i]=-;
end[sz]=;
sz++;
return sz-;
}
void clear(){
sz=;root=newnode();
}
int num(char c){
if (c=='A') return ;
else
if (c=='T') return ;
else
if (c=='G') return ;
else
if (c=='C') return ;
return ;
}
void insert(){
scanf("%s",s);
int len=strlen(s);
int now=root;
for (int i=;i<len;i++){
if (ch[now][num(s[i])]==-) ch[now][num(s[i])]=newnode();
now=ch[now][num(s[i])];
}
end[now]=;
}
void build(){
int now=root;
std::queue<int>Q;
fail[root]=root;
for (int i=;i<=;i++)
if (ch[root][i]==-) ch[root][i]=root;
else fail[ch[root][i]]=root,Q.push(ch[root][i]);
while (!Q.empty()){
int now=Q.front();Q.pop();
if (end[fail[now]]==) end[now]|=;
for (int i=;i<=;i++){
if (ch[now][i]==-) ch[now][i]=ch[fail[now]][i];
else fail[ch[now][i]]=ch[fail[now]][i],Q.push(ch[now][i]);
}
}
}
void dp(){
scanf("%s",s);
int len=strlen(s);
for (int i=;i<=len;i++)
for (int j=;j<=sz;j++)
f[i][j]=;
f[][root]=;
for (int i=;i<len;i++)
for (int j=;j<sz;j++)
if (f[i][j]<){
for (int k=;k<=;k++){
int now=ch[j][k];
if (end[now]) continue;
int tmp;
if (k==num(s[i])) tmp=f[i][j];
else tmp=f[i][j]+;
f[i+][now]=std::min(f[i+][now],tmp);
}
}
int ans=;
for (int i=;i<sz;i++)
ans=std::min(ans,f[len][i]);
if (ans==) ans=-;
printf("%d\n",ans);
}
int main(){
while (scanf("%d",&n)!=EOF){
if (n==) return ;
Tcase++;
printf("Case %d: ",Tcase);
clear();
for (int i=;i<=n;i++)
insert();
build();
dp();
}
}
HDU 2457 DNA repair (AC自动机+DP)的更多相关文章
- 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: ...
- HDU 2457/POJ 3691 DNA repair AC自动机+DP
DNA repair Problem Description Biologists finally invent techniques of repairing DNA that contains ...
- 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串,给出一个原串,问最少需要修改多少个字符,使得原串中不包含非法串. 解题关键:多模式串匹配->AC自动机,求最优值->dp,注意在AC自动机上dp的套路. ...
- POJ 3691 DNA repair(AC自动机+DP)
题目链接 能AC还是很开心的...此题没有POJ2778那么难,那个题还需要矩阵乘法,两个题有点相似的. 做题之前,把2778代码重新看了一下,回忆一下当时做题的思路,回忆AC自动机是干嘛的... 状 ...
- POJ3691 DNA repair(AC自动机 DP)
给定N个长度不超过20的模式串,再给定一个长度为M的目标串S,求在目标串S上最少改变多少字符,可以使得它不包含任何的模式串 建立Trie图,求得每个节点是否是不可被包含的串,然后进行DP dp[i][ ...
- Hdu 2457 DNA repair (ac自己主动机+dp)
题目大意: 改动文本串的上的字符,使之不出现上面出现的串.问最少改动多少个. 思路分析: dp[i][j]表示如今 i 个字符改变成了字典树上的 j 节点. 然后顺着自己主动机一直转移方程. 注意合法 ...
- HDU 2457 DNA repair(AC自动机+DP)题解
题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...
- hdu 4117 GRE Words AC自动机DP
题目:给出n个串,问最多能够选出多少个串,使得前面串是后面串的子串(按照输入顺序) 分析: 其实这题是这题SPOJ 7758. Growing Strings AC自动机DP的进阶版本,主题思想差不多 ...
随机推荐
- pfsense 2.2RC下的L2TP配置
还不有测试完成,不过,基本上应该差不多了. 主要参考以下文档: http://blog.sina.com.cn/s/blog_541a3cf10101ard3.html http://thepract ...
- LeetCode_Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- HDU5584 LCM Walk 数论
LCM Walk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- Linux中部署JAVA程序
JAVA程序在开发完成后,需要部署到服务器,如果是WEB项目,需要部署到WEB服务器,否则部署到应用服务器. JAVA是跨平台的编程语言,服务器的操作系统可以是Windows.Linux或者其它,下面 ...
- php字符串常用处理函数(数组的拆分、查找替换)
//字符串常用函数 $a = "hello"; echo strlen($a); //输出字符串的长度 $b = "Hello"; ...
- Python Scrapy安装杂症记录
昨天安装了scrapy一切正常,调试了bbsSpider案例(详见上文),今日开机因为冰封还原,提示找不到python27.dll,重新安装了python2.7, 使用easy-install scr ...
- NUnit - 使用感受
Nunit使用 最近项目开始大量使用Nunit, 发现Nunit还是有很多好处的. 1. 测试驱动逻辑,这样可以尽最大可能减少“修改”引入的Bug. 如果你修改了一些东西,导致Case跑不过.请检查你 ...
- [Matlab] Attempt to execute SCRIPT *** as a function
Attempt to execute SCRIPT *** as a function 问题: 在运行MATLAB程序的时候,出现如题的报错. 原因: 在系统中,现有的.m文件有的与***函数重名,所 ...
- 利用libevent的timer实现定时器interval
在不怎么了解libevent的情况下,看到timer这个关键字想到可以用来做定时任务,官方资料也不齐全,就从代码里看到了TIMEOUT字样,这么说来应该是支持timeout了,那interval呢,貌 ...
- pyqt学习之列表管理器(网友提供)
# -*- coding: utf-8 -*- __author__ = 'Administrator' import sys from PyQt4.QtCore import * from PyQt ...