直接从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. 【BZOJ 3942】 Censoring

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3942 [算法] 栈 + KMP [代码] #include<bits/stdc ...

  2. Coursera Algorithms week2 栈和队列 练习测验: Stack with max

    题目原文: Stack with max. Create a data structure that efficiently supports the stack operations (push a ...

  3. [Swift]LeetCode1073. 负二进制数相加 | Adding Two Negabinary Numbers

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  4. C99新增内容之复合文字(compound literal)

    前言: 最近在复习C,发现了一些新东西,例如:变长数组,复合文字,指针的兼容性等.今天先简单谈一下复合文字. 正文: 假如需要向带有一个int参量的函数传递一个值,您可以传递一个int变量,也可以传递 ...

  5. SQL Server中char与varchar数据类型区别

    在SQL Server中char类型的长度是不可变的,而varchar的长度是可变的 . 存入数据时: 如果数据类型为char时,当定义一个字段固定长度时,如果存进去数据长度小于char的长度,那么存 ...

  6. android黑科技系列——微信定位聊天记录中照片的位置信息插件开发详解

    一.前言 最近关于微信中,朋友之间发送原图就可能暴露你的位置信息,其实这个问题不在于微信,微信是为了更好的体验效果,才有发送原图功能,而对于拍照,发送普通图片微信后台都会过滤图片的exif信息,这样就 ...

  7. 01--Java IO基础

    一.java.io包概览 Java IO包主要可以分为如下4类: 基于字节操作的I/O接口:InputStream和OutputStream. 基于字符操作的I/O接口:Writer和Reader 基 ...

  8. ArrayList 源码

    1.ArrayList的类关系: 2.属性及方法      2.1 构造           三个构造方法分别对应:                通过传入初始化容器大小构造数组列表         ...

  9. Python3编写Windows服务程序

    最近做了公司签到的小工具,有同事要求做成Windows服务,开机自启.先说下怎么用Python写Windows服务程序. #encoding=utf-8 import win32serviceutil ...

  10. git与pycharm结合使用

    一.配置pycharm 在pycharm中选择file-->setting,在弹出的窗口中选择version control,选择git,配置git的路径 将当前项目关闭 在弹出的窗口中选择ch ...