题:http://acm.hdu.edu.cn/showproblem.php?pid=2457

题意:给定n个模式串,给定一个主串,问最替换掉多少个字符使主串不包含模式串或输出“-1”表示没有可行的方案;

分析:给n个模式串建立ac自动机,考虑dp[i][j],表示长度为 i , j 节点变换为主串前 i 个的最小操作数,j节点要转换必须使当前节点为“安全点”,即end[trie[i][j]]==0;

   dp转化就只要不是自己就要在转移过程中+1,i 位置取min 给下一位i+1

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const int M=2e3+;
const int maxn=;
const int inf=0x3f3f3f3f;
int dp[M][M];
char s[M];
struct ac{
int trie[M][maxn],fail[M];
bool end[M];
int tot,root;
int newnode(){
for(int i=;i<maxn;i++)
trie[tot][i]=-;
end[tot++]=;
return tot-;
}
void init(){
memset(end,false,sizeof(end));
tot=;
root=newnode();
}
int getid(char c){
if(c=='A')
return ;
if(c=='G')
return ;
if(c=='T')
return ;
if(c=='C')
return ;
}
void insert(char buf[]){
int now=root,len=strlen(buf);
for(int i=;i<len;i++){
int id=getid(buf[i]);
if(trie[now][id]==-)
trie[now][id]=newnode();
now=trie[now][id];
}
end[now]=true;
}
void getfail(){
queue<int>que;
while(!que.empty())
que.pop();
fail[root]=root;
for(int i=;i<maxn;i++){
if(trie[root][i]==-)
trie[root][i]=root;
else{
fail[trie[root][i]]=root;
que.push(trie[root][i]);
}
}
while(!que.empty()){
int now=que.front();
que.pop();
if(end[fail[now]])
end[now]=true;
for(int i=;i<maxn;i++){
if(trie[now][i]!=-){
fail[trie[now][i]]=trie[fail[now]][i];
que.push(trie[now][i]);
}
else
trie[now][i]=trie[fail[now]][i];
}
}
}
}AC;
int main(){
int n,t=;
while(~scanf("%d",&n)&&n){
AC.init();
for(int i=;i<n;i++){
scanf("%s",s);
AC.insert(s);
}
AC.getfail(); scanf("%s",s);
int len=strlen(s);
for(int i=;i<=len;i++)
for(int j=;j<AC.tot;j++)
dp[i][j]=inf;
dp[][AC.root]=;
for(int i=;i<len;i++)
for(int j=;j<AC.tot;j++)
if(dp[i][j]<inf){
for(int k=;k<maxn;k++){
int now=AC.trie[j][k];
if(AC.end[now])
continue;
// cout<<now<<"!!"<<endl;
int id=AC.getid(s[i]);
int add=;
if(id!=k)
add++;
dp[i+][now]=min(dp[i+][now],dp[i][j]+add);
// cout<<dp[i+1][now];
}
}
int ans=inf;
for(int i=;i<AC.tot;i++){
ans=min(ans,dp[len][i]); } printf("Case %d: ",++t);
if(ans==inf)
puts("-1");
else
printf("%d\n",ans);
}
return ;
}

hdu2457(最少替换多少个字符使主串不包含模式串)ac自动机+dp的更多相关文章

  1. 【hdu2457】ac自动机 + dp

    传送门 题目大意: 给你一个字符主串和很多病毒串,要求更改最少的字符使得没有一个病毒串是主串的子串. 题解: ac自动机 + dp,用病毒串建好ac自动机,有毒的末尾flag置为true 构建fail ...

  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. HDU2457 DNA repair —— AC自动机 + DP

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

  5. linux(centos8):用tr替换或删除字符

    一,tr命令的用途 tr命令可以替换或删除文件中的字符 它从标准输入设备读取数据, 处理完成将结果输出到标准输出设备 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnbl ...

  6. poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】

    Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 503 ...

  7. 【Linux基础】tr命令替换和删除字符

    1.tr命令 tr可以对来自标准输入的字符进行替换.压缩和删除,可以将一组字符变成另外一组字符.通过使用 tr,您可以非常容易地实现 sed 的许多最基本功能.您可以将 tr 看作为 sed 的(极其 ...

  8. vim的全局替换[zz]&把字符替换成回车

    本文出自   http://blog.csdn.net/shuangde800   本文是在学习<使用vi编辑器, Lamb & Robbins编著>时在所记的笔记.   本文内容 ...

  9. mysql主从怎么样使主为innodb辅为myisam

    MySQL主从复制(linux主+windows从) http://blog.csdn.net/qq_20032995/article/details/54380290 mysql主从怎么样使主为in ...

随机推荐

  1. 盘姬工具箱WV1.10

    ========================================================================== {盘姬工具箱CruiserEXPforWin版是一 ...

  2. Mysql 模糊查询 转义字符

    MySQL的转义字符“\” \0   一个ASCII  0  (NUL)字符.    \n    一个新行符.    \t    一个定位符.    \r    一个回车符.    \b    一个退 ...

  3. POJ 3614:Sunscreen 贪心+优先队列

    Sunscreen Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5378   Accepted: 1864 Descrip ...

  4. SQL语句--分组统计

    一.教师号 星期号 是否有课1 2 有1 3 有2 1 有3 2 有`1 2 有写一条sql语句让你变为这样的表教师号 星期一 星期二 星期三1 2 12 13 1各星期下的数字表示:对应的教师在星期 ...

  5. python csv

    Python 读取csv的某行 Python 读取csv的某列 Python写了一个可以提取csv任一列的代码,欢迎使用.Github链接 两个list写入csv文件 column1,column2 ...

  6. 解决Exception: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z

    在项目中添加src中添加NativeIO类 /** * Licensed to the Apache Software Foundation (ASF) under one * or more con ...

  7. php.laravel.部署

    Laravel 的部署 D  参考laravel-china的做 | 其他参考 | 重要的参考 | Nginx github | 如果想要ssl 啊

  8. TensorFlow中的L2正则化函数:tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()的用法与异同

    tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()都是TensorFlow中的L2正则化函数,tf.contrib.layers.l2_regula ...

  9. Vulkan SDK 之 Graphics Pipeline

    A graphics pipeline consists of shader stages, a pipeline layout, a render pass, and fixed-function ...

  10. JavaWeb高级编程(下篇)

    Java标准标签库 JSP标签语法中包含一些简写可以帮助轻松编写JSP.这些简写中第一个就是taglib指令. <%@ taglib prefix="c" uri=" ...