hdu2457(最少替换多少个字符使主串不包含模式串)ac自动机+dp
题: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的更多相关文章
- 【hdu2457】ac自动机 + dp
传送门 题目大意: 给你一个字符主串和很多病毒串,要求更改最少的字符使得没有一个病毒串是主串的子串. 题解: ac自动机 + dp,用病毒串建好ac自动机,有毒的末尾flag置为true 构建fail ...
- HDU2457 DNA repair(AC自动机+DP)
题目一串DNA最少需要修改几个基因使其不包含一些致病DNA片段. 这道题应该是AC自动机+DP的入门题了,有POJ2778基础不难写出来. dp[i][j]表示原DNA前i位(在AC自动机上转移i步) ...
- [hdu2457]DNA repair(AC自动机+dp)
题意:给出一些不合法的模式DNA串,给出一个原串,问最少需要修改多少个字符,使得原串中不包含非法串. 解题关键:多模式串匹配->AC自动机,求最优值->dp,注意在AC自动机上dp的套路. ...
- HDU2457 DNA repair —— AC自动机 + DP
题目链接:https://vjudge.net/problem/HDU-2457 DNA repair Time Limit: 5000/2000 MS (Java/Others) Memory ...
- linux(centos8):用tr替换或删除字符
一,tr命令的用途 tr命令可以替换或删除文件中的字符 它从标准输入设备读取数据, 处理完成将结果输出到标准输出设备 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnbl ...
- poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10141 Accepted: 503 ...
- 【Linux基础】tr命令替换和删除字符
1.tr命令 tr可以对来自标准输入的字符进行替换.压缩和删除,可以将一组字符变成另外一组字符.通过使用 tr,您可以非常容易地实现 sed 的许多最基本功能.您可以将 tr 看作为 sed 的(极其 ...
- vim的全局替换[zz]&把字符替换成回车
本文出自 http://blog.csdn.net/shuangde800 本文是在学习<使用vi编辑器, Lamb & Robbins编著>时在所记的笔记. 本文内容 ...
- mysql主从怎么样使主为innodb辅为myisam
MySQL主从复制(linux主+windows从) http://blog.csdn.net/qq_20032995/article/details/54380290 mysql主从怎么样使主为in ...
随机推荐
- Delphi IDFtp用法,包含断点续传
1 连接远程服务器procedure Connect(AAutoLogin: boolean; const ATimeout: Integer);2 改变目录procedure ChangeDir ...
- 08 SSM整合案例(企业权限管理系统):05.SSM整合案例的基本介绍
04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.权限控制 09.用户和角色操作 10.权限关联 11.AOP日志 05.SSM整合案例的基本介绍 ...
- writeObiect与序列化反序列化
在写仿QQ聊天工具中,客户端与服务端之间的通信一开始是采用的是InputStream和OutputStream,这导致在数据传输过程中,登录信息,注册信息等难以区分,这时我给传输的数据加了标识来分辨信 ...
- C语言拾遗——strtok
C语言拾遗——strtok 今天刷PAT的时候用到了这个strtok函数,顺手就记录一下 strtok函数包含于头文件string.h 语法:char *strtok( char *str1, con ...
- POJ 2305:Basic remains 进制转换
Basic remains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5221 Accepted: 2203 Des ...
- Java8 Stream分组
//根据排课id分组 Map<Integer, List<Schedule4Homework>> idSchedule4HomeworksMap = schedule4Home ...
- mongoose 报错:DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead
mongoose.set('useCreateIndex', true) // 加上这个
- postman测试带有json数据格式的字段
测试六个字段 普通字段: ModelCode 普通字段: MmodelCode 普通字段: ModelTagKey 普通字段: ModelTagValue 普通字段: ModelTagType jso ...
- json字符串格式化显示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 第十八篇 admin组件
admin组件 admin组件使用 admin源码解析 admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以 ...