HDU-3247 Resource Archiver(AC自动机+BFS)
Description
Wait a minute… you realized that it isn’t as easy as you thought. Think about the virus killers. They’ll find your software suspicious, if your software contains one of the m predefined virus codes. You absolutely don’t want this to happen.
Technically, resource files and virus codes are merely 01 strings. You’ve already convinced yourself that none of the resource strings contain a virus code, but if you make the archive arbitrarily, virus codes can still be found somewhere.
Here comes your task (formally): design a 01 string that contains all your resources (their occurrences can overlap), but none of the virus codes. To make your software smaller in size, the string should be as short as possible.
Input
Output
Sample Input
2 2
1110
0111
101
1001
0 0
Sample Output
5 题目大意:给n个资源01串和m个病毒01串。构造一个最短的01串使其包含所有的资源串,但不包含任何一个病毒串。
题目分析:建立好AC自动机后,在上面广搜即可。 代码如下:
# include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std; const int N=10000; int ch[6*N+5][2];
int fail[6*N+5];
int sz,type[6*N+5]; void init()
{
sz=0;
memset(ch,-1,sizeof(ch));
memset(type,0,sizeof(type));
} int idx(char c)
{
return c-'0';
} void insert(char *s,int val)
{
int r=0;
int n=strlen(s);
for(int i=0;i<n;++i){
int c=idx(s[i]);
if(ch[r][c]==-1) ch[r][c]=++sz;
r=ch[r][c];
}
type[r]=val;
} void getFail()
{
queue<int>q;
fail[0]=0;
for(int i=0;i<2;++i){
if(ch[0][i]==-1)
ch[0][i]=0;
else{
q.push(ch[0][i]);
fail[ch[0][i]]=0;
}
}
while(!q.empty())
{
int u=q.front();
q.pop();
if(type[fail[u]]>0)
type[u]|=type[fail[u]];
for(int i=0;i<2;++i){
if(ch[u][i]==-1)
ch[u][i]=ch[fail[u]][i];
else{
fail[ch[u][i]]=ch[fail[u]][i];
q.push(ch[u][i]);
}
}
}
} struct Node
{
int step;
int loca;
int sta;
};
int n,m;
char s[1005];
char vis[6*N+5][1<<10]; int bfs()
{
queue<Node>q;
memset(vis,0,sizeof(vis));
q.push(Node{0,0,0});
while(!q.empty())
{
Node u=q.front();
q.pop();
if(u.sta==(1<<n)-1)
return u.step;
for(int i=0;i<2;++i) if(type[ch[u.loca][i]]>=0){
int v=ch[u.loca][i];
if(vis[v][u.sta|type[v]]) continue;
vis[v][u.sta|type[v]]=1;
q.push(Node{u.step+1,v,u.sta|type[v]});
}
}
return -1;
} int main()
{
while(~scanf("%d%d",&n,&m)&&(n+m))
{
init();
for(int i=0;i<n;++i){
scanf("%s",s);
insert(s,1<<i);
}
for(int i=0;i<m;++i){
scanf("%s",s);
insert(s,-1);
}
getFail();
printf("%d\n",bfs());
}
return 0;
}
HDU-3247 Resource Archiver(AC自动机+BFS)的更多相关文章
- HDU 3247 Resource Archiver (AC自动机+BFS+状压DP)
题意:给定 n 个文本串,m个病毒串,文本串重叠部分可以合并,但合并后不能含有病毒串,问所有文本串合并后最短多长. 析:先把所有的文本串和病毒都插入到AC自动机上,不过标记不一样,可以给病毒标记-1, ...
- HDU - 3247 Resource Archiver (AC自动机,状压dp)
\(\quad\)Great! Your new software is almost finished! The only thing left to do is archiving all you ...
- HDU3247 Resource Archiver —— AC自动机 + BFS最短路 + 状压DP
题目链接:https://vjudge.net/problem/HDU-3247 Resource Archiver Time Limit: 20000/10000 MS (Java/Others) ...
- HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解
题意:目标串n( <= 10)个,病毒串m( < 1000)个,问包含所有目标串无病毒串的最小长度 思路:貌似是个简单的状压DP + AC自动机,但是发现dp[1 << n][ ...
- HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...
- hdu_3247_Resource Archiver(AC自动机+bfs+TSP)
题目链接:hdu_3247_Resource Archiver 题意: 有n个资源串,m个病毒串,现在要将所有的资源串整合到一个串内,并且这个串不能包括病毒串,问最短的串长为多少 题解: 将资源串和病 ...
- HDU3247 Resource Archiver (AC自动机+spfa+状压DP)
Great! Your new software is almost finished! The only thing left to do is archiving all your n resou ...
- hdu 2896 病毒侵袭 ac自动机
/* hdu 2896 病毒侵袭 ac自动机 从题意得知,模式串中没有重复的串出现,所以结构体中可以将last[](后缀链接)数组去掉 last[]数组主要是记录具有相同后缀模式串的末尾节点编号 .本 ...
- hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Resource Archiver HDU - 3247 AC自动机+BFS+状压
题意: 给出n个资源串,m个病毒串,现在要如何连接资源串使得不含病毒串(可以重叠,样例就是重叠的). 题解: 这题的套路和之前的很不同了,之前的AC自动机+DP的题目一般都是通过teir图去转移, 这 ...
随机推荐
- java基础之 多态
在面向对象编程(Object-Oriented Programming, OOP)中,多态机制无疑是其最具特色的功能,甚至可以说,不运用多态的编程不能称之为OOP.这也是为什么有人说,使用面向对象语言 ...
- c语言问卷调查
你对自己的未来有什么规划?做了哪些准备? 找一家公司实习积攒创业基金.努力学习专业知识,从各方面完善自身,参与各项活动如辩论赛,竞赛等锻炼自己. 2.你认为什么是学习?学习有什么用?现在学习动力如何? ...
- PHP JS JQ 异步上传并立即显示图片
提交页面: <! DOCTYPE html> < html> < head> < meta charset ="GB2312" > ...
- 官网app下载更换成微信公众号二维码 测试
微信现在很火啊.公司官网原先提供的ios和andriod的app下载链接要求切换成微信公众号二维码.简单的替换,大家都说不需要测试直接上线.还是测了下. 1 验证所有与下载相关的信息都已去除. 包括下 ...
- Python的平凡之路(14)
一.什么是HTML HTML(Hyper Text Mark-up Language ) 即超文本标记语言,是 WWW 的描述语言,由 Tim Berners-lee提出.设计 HTML 语言的目的是 ...
- Spring3.0 与 MyBatis框架 整合小实例
本文将在Eclipse开发环境下,采用Spring MVC + Spring + MyBatis + Maven + Log4J 框架搭建一个Java web 项目. 1. 环境准备: 1.1 创建数 ...
- 曲线参数化的Javascript实现(理论篇)
在关键帧动画的制作过程中,动画师在k物体运动的过程中,一般要确定2个参数: 1)运动轨迹(表示物体运动的路径): 2)速度曲线(表示物体随时间的速度变化). 对于运动轨迹通常选用一定的样条曲线,通过动 ...
- 几款Z2760平板对比
现阶段的Windows平板本质上分为Windows RT系统平板和完整的Windows 8系统平板两大阵营.RT系统的平板轻薄续航持久,但是由于没法安装常规的.exe程序,所以对于工作需要略显不足,是 ...
- Java 和 C+
文不对题,啦啦啦~~~ 第一次感到在windows平台java应用发布的无力,平时自己自写自用都是在自己电脑上,没什么感觉.如今要发布个给人用,打包应用.附加jre,这过程还得多加几行字说明,另人特么 ...
- iOS应用性能调优好文mark
http://www.cocoachina.com/ios/20150408/11501.html