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图去转移, 这 ...
随机推荐
- SharePoint Site "Language Settings"功能与CSOM的对应
博客地址:http://blog.csdn.net/FoxDave SharePoint网站中的语言设置:"Language Settings",可以用CSOM通过Site的一些 ...
- TestNG插件的安装问题
一.可以采用离线安装的方式 离线安装若不成功,可以删除之前的eclipse,然后在新下载eclipse中添加离线安装包 1.离线安装方法: 发现很多同学和我一样无法在线安装testNg,现在分享一个离 ...
- 触发Full GC执行的情况
除直接调用System.gc外,触发Full GC执行的情况有如下四种. 1. 旧生代空间不足 旧生代空间只有在新生代对象转入及创建为大对象.大数组时才会出现不足的现象,当执行Full GC后空间仍然 ...
- Page事件执行顺序
Page 执行中将按照如下顺序激活事件: Page.PreInit Page.Init Page.InitComplite Page.PreLoad Page.Load Page.LoadComple ...
- C# 时间计算 今天、昨天、前天、明天 一个月的开始日期与结束日期
C# 时间计算 今天.昨天.前天.明天 class Program { static void Main(string[] args) { ...
- 【转】C#线程同步示例
using System; using System.Threading; // 银行帐户类 class Account { int balance; ...
- ubuntu用下载的文件替换即可更新
./usr/lib/flashplugin-installer/libflashplayer.so 也有可能是 /usr/lib/firefox-addons/plugins
- htop基本使用
一.什么是htop? top是所有类unix系统的必备工具,能直观方便的查看到系统负载.内存及进程等信息. 而htop具有top工具的全部功能且还新增了一些额外的功能和使用体验改进.与top相比,其具 ...
- Shiro安全登录框架
环境准备 本文使用Maven构建,因此需要一点Maven知识.首先准备环境依赖: <dependencies> <dependency> <groupId>juni ...
- GridView获取CheckBox的值及所在列的ID
//根据GridView的列数进行循环 ; i < GridView1.Rows.Count; i++) { //检查是否有ID为CheckBox1的CheckBox控件,如果有就赋值给Chec ...