直接从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. 74.资金管理-员工工资配置 extjs 页面

    1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&quo ...

  2. springboot踩坑出坑记

    4月15到4月17我都在把毕设从eclipse重构到IDEA中,springboot最让我头疼的是它的版本问题,因为每一个版本对应的依赖包都有可能出错,这里分享一下如何成功移植用eclipse写的sp ...

  3. 使用JQuery制作幻灯片(轮播图)

    1.首先看一下目录结构 images文件夹放所需要播放的图片. js文件夹放jquery库和main.js 2.html代码: <!DOCTYPE html> <html lang= ...

  4. php 提交编辑数据没有变,返回0,判断

    php 提交编辑数据没有变,返回0,判断以TP为例子 $edit = D('Brand')->save($data);if($edit == true){ echo "修改成功&quo ...

  5. Mysql数据库系列

    详情点击 MySQL基础 Mysql表操作 Mysql插入 更新 删除 查询操作 Mysql创建用户和授权 基本的Mysql语句 Mysql库的操作 Mysql表的操作 Mysql数据类型(一) My ...

  6. BZOJ 1511 KMP

    题意:求出每个前缀的最长周期之和(等于本身的算0) 思路: 求出来next数组  建出next树 找到不为0的最小的 n减去它就是答案 //By SiriusRen #include <cstd ...

  7. 2C课程笔记分享_StudyJams_2017

    课程2C-实践:创建交互式应用 概述 课程2C的内容主要是练习巩固2A.2B中讲解的内容,并设计实现一款篮球比赛的计分板应用及其界面的美化. Warm-Up:准备活动 新建项目PracticeSet2 ...

  8. Android,加载离线Android API文档缓慢问题!

    解决方法:在host文件末添加如下信息! 0.0.0.0 www.googleapis.com 0.0.0.0 www.google.com 0.0.0.0 www.google-analytics. ...

  9. dubbo之回声测试

    回声测试 回声测试用于检测服务是否可用,回声测试按照正常请求流程执行,能够测试整个调用是否通畅,可用于监控. 所有服务自动实现 EchoService 接口,只需将任意服务引用强制转型为 EchoSe ...

  10. CXF-JAX-WS开发(一)入门案例

    一.Web Service 1.定义 W3C定义,Web服务(Web service)应当是一个软件系统,用以支持网络间不同机器的互动操作. 2.作用 多系统间数据通信 二.CXF是什么? CXF是目 ...