直接从root遍历扩展DP,当扩展到的字母和字符串字母相同时,不用修改,不同时,要求修改加1

注意不要扩展危险结点。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string.h>
#include <queue>
#include <cmath>
#include <map>
#include <vector>
#define LL __int64
using namespace std; const int Maxn=1010;
const int dictsize=4;
const int root=0;
const int inf=(1<<30);
int ids['Z'];
int fail[Maxn],trie[Maxn][dictsize];
bool tag[Maxn];
int head,tail,tot;
int que[Maxn];
char str[Maxn];
int n;
int dp[Maxn][Maxn]; void Insert_trie(){
int p=0,i=0,index;
while(str[i]){
index=ids[str[i]];
if(trie[p][index]==-1) trie[p][index]=++tot;
p=trie[p][index];
i++;
}
tag[p]=true;
} void build_ac(){
head=tail=0;
que[tail++]=root;
while(head!=tail){
int tmp=que[head++];
int p=-1;
for(int i=0;i<dictsize;i++){
if(trie[tmp][i]!=-1){
if(tmp==root) fail[trie[tmp][i]]=root;
else{
p=fail[tmp];
while(p!=-1){
if(trie[p][i]!=-1){
fail[trie[tmp][i]]=trie[p][i];
break;
}
p=fail[p];
}
if(p==-1) fail[trie[tmp][i]]=root;
}
if(tag[fail[trie[tmp][i]]]) tag[trie[tmp][i]]=true;
que[tail++]=trie[tmp][i];
}
else{ //trie[tmp][i]==-1
if(tmp==root) trie[tmp][i]=root;
else{
p=fail[tmp];
while(p!=-1){
if(trie[p][i]!=-1){
trie[tmp][i]=trie[p][i];
break;
}
p=fail[p];
}
if(p==-1) trie[tmp][i]=root;
}
}
}
}
} int main(){
int T=0;
ids['A']=0,ids['C']=1,ids['G']=2,ids['T']=3;
while(scanf("%d",&n)&&n){
head=tail=tot=0;
memset(fail,-1,sizeof(fail));
memset(trie,-1,sizeof(trie));
memset(tag,false,sizeof(tag));
for(int i=0;i<n;i++){
scanf("%s",str);
Insert_trie();
}
scanf("%s",str+1);
build_ac();
int len=strlen(str+1);
memset(dp,-1,sizeof(dp));
dp[0][0]=0; int son;
for(int j=0;j<len;j++){
for(int i=0;i<=tot;i++){
if(dp[i][j]>=0){
for(int k=0;k<dictsize;k++){
if(!tag[trie[i][k]]){
son=trie[i][k];
if(k==ids[str[j+1]]){
if(dp[son][j+1]==-1){
dp[son][j+1]=dp[i][j];
}
else{
dp[son][j+1]=min(dp[i][j],dp[son][j+1]);
}
}
else{
if(dp[son][j+1]==-1){
dp[son][j+1]=dp[i][j]+1;
}
else{
dp[son][j+1]=min(dp[i][j]+1,dp[son][j+1]);
}
}
}
}
}
}
}
int ans=inf;
for(int i=0;i<=tot;i++){
if(dp[i][len]!=-1){
ans=min(ans,dp[i][len]);
}
}
printf("Case %d: %d\n",++T,ans==inf?-1:ans);
}
return 0;
}

  

HDU 2457的更多相关文章

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

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

  2. 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: ...

  3. hdu 2457(ac自动机+dp)

    题意:容易理解... 分析:这是一道比较简单的ac自动机+dp的题了,直接上代码. 代码实现: #include<stdio.h> #include<string.h> #in ...

  4. hdu 2457 DNA repair

    AC自动机+DP.按着自动机跑,(其实是生成新的满足题目要求的串,然后找改变最少的.)但是不能跑到是单词的地方,如果跑到单词的话那么说明改变后的串含有病毒了,不满足题意.然后就是应该怎么跑的问题了,现 ...

  5. DNA repair - HDU 2457(自动机+dp)

    题目大意:给你N个DNA的串,也就是至包含'A','T','G','C'四种碱基的,这些给定的串都是带有遗传病的,然后给你一个不会超过1000的串,问你至少几个地方才能让这个串不包含遗传病,如果不论怎 ...

  6. HDU 2457 DNA repair (AC自动机+DP)

    题意:给N个串,一个大串,要求在最小的改变代价下,得到一个不含上述n个串的大串. 思路:dp,f[i][j]代表大串中第i位,AC自动机上第j位的最小代价. #include<algorithm ...

  7. Hdu 2457 DNA repair (ac自己主动机+dp)

    题目大意: 改动文本串的上的字符,使之不出现上面出现的串.问最少改动多少个. 思路分析: dp[i][j]表示如今 i 个字符改变成了字典树上的 j 节点. 然后顺着自己主动机一直转移方程. 注意合法 ...

  8. HDU 2457/POJ 3691 DNA repair AC自动机+DP

    DNA repair Problem Description   Biologists finally invent techniques of repairing DNA that contains ...

  9. DNA repair HDU - 2457 AC自动机+DP

    题意: 给你N个模板串,并且给你一个文本串, 现在问你这个文本串最少需要改变几个字符才能使得它不包含任何模板串. (以上字符只由A,T,G,C构成) 题解: 刚开始做这一题的时候表示很懵逼,好像没有学 ...

随机推荐

  1. 线性回归(最小二乘法、批量梯度下降法、随机梯度下降法、局部加权线性回归) C++

    We turn next to the task of finding a weight vector w which minimizes the chosen function E(w). Beca ...

  2. Building a Space Station(bfs)

    http://poj.org/problem?id=2031 题意:给出n个球的圆心坐标x,y,z, 半径r,若任意两球不相交,则在两球间建桥.问需建桥的最短距离是多少. 思路:建图,以两球间相差的距 ...

  3. js getyear和getfullyear

    getyear()函数如果今年是2015年会得到115,getfullyear()会得到完整的年份.

  4. nodejs express开发

    用NodeJS+Express开发WEB应用---第一篇 大漠穷秋2014-03-28 预热 为了对后面的内容理解更加透彻,推荐首先阅读下面这篇很好的文章: http://www.nodebeginn ...

  5. java常见面试题03-String,StringBuffer,StringBuilder的区别

    面试题   A:String,StringBuffer,StringBuilder的区别 1:String 内容不可变,StringBuffer.StringBudiler可变  2:StringBu ...

  6. Unity中内嵌网页插件UniWebView

    一.常见Unity中内嵌网页实现方式: 1.UnityWebCore只支持windows 2.Unity-Webview支持Android,IOS 3.UniWebView支持mac os,Andro ...

  7. BZOJ 2118 Dijkstra

    思路: 经典题 不解释 找到最小的数mn 所有都是在mod mn的意义下 搞得 i->(i+a[i])%mn  边权为a[i] //By SiriusRen #include <queue ...

  8. C - cAPS lOCK

    Problem description wHAT DO WE NEED cAPS LOCK FOR? Caps lock is a computer keyboard key. Pressing it ...

  9. 【JAVA练习】- 一个逻辑题

    打印 1 3    4 5   8    12 7   12   20   32 9    16  28    48   80  ..... 输入任意一个奇数,输出那一行的数据 第一种方法找到规律进行 ...

  10. 【PL/SQL】匿名块、存储过程、函数、触发器

    名词解释 子程序:PL/SQL的过程和函数统称为子程序. 匿名块:以DECLARE或BEGIN开始,每次提交都被编译.匿名块因为没有名称,所以不能在数据库中存储并且不能直接从其他PL/SQL块中调用. ...