题意:目标串n( <= 10)个,病毒串m( < 1000)个,问包含所有目标串无病毒串的最小长度

思路:貌似是个简单的状压DP + AC自动机,但是发现dp[1 << n][5e4]根本开不出那么多空间,似乎GG。但是我们仔细想一下就能发现,既然要包含所有目标串的最小长度,那必然这个串就是只有目标串叠加组成的,只是在叠加的过程中我们不能混入病毒串。所以其实Trie树上有用的点最多就10个,我们只要处理出所有目标串之间“最小有效转化”就行了,那么空间为dp[1 << n][10]。

处理目标串之间“最小有效转化”可以直接暴力BFS,build标记后在Trie树上跑。

代码:

#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<string>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 6e4 + 5;
const int M = 50 + 5;
const ull seed = 131;
const int INF = 0x3f3f3f3f;
const int MOD = 20090717;
int n, m;
int dp[1 << 10 + 5][20];
int point[1015];
int length[1015];
int dis[20][20];
int vis[maxn];
int tot;
struct Node{
int u, step;
};
struct Aho{
struct state{
int next[2];
int fail, cnt, is;
}node[maxn];
int size;
queue<int> q; void init(){
size = 0;
newtrie();
while(!q.empty()) q.pop();
} int newtrie(){
memset(node[size].next, 0, sizeof(node[size].next));
node[size].cnt = node[size].fail = 0;
return size++;
} void insert(char *s, int id){
int len = strlen(s);
int now = 0;
for(int i = 0; i < len; i++){
int c = s[i] - '0';
if(node[now].next[c] == 0){
node[now].next[c] = newtrie();
}
now = node[now].next[c];
}
node[now].cnt = 1 << id;
node[now].is = tot;
length[tot] = len;
point[tot++] = now; } void build(){
node[0].fail = -1;
q.push(0); while(!q.empty()){
int u = q.front();
q.pop();
if(node[node[u].fail].cnt && u) node[u].cnt |= node[node[u].fail].cnt;
for(int i = 0; i < 2; i++){
if(!node[u].next[i]){
if(u == 0)
node[u].next[i] = 0;
else
node[u].next[i] = node[node[u].fail].next[i];
}
else{
if(u == 0) node[node[u].next[i]].fail = 0;
else{
int v = node[u].fail;
while(v != -1){
if(node[v].next[i]){
node[node[u].next[i]].fail = node[v].next[i];
break;
}
v = node[v].fail;
}
if(v == -1) node[node[u].next[i]].fail = 0;
}
q.push(node[u].next[i]);
}
}
}
} void bfs(int x){
queue<Node> Q;
while(!Q.empty()) Q.pop();
memset(vis, 0, sizeof(vis));
dis[x][x] = 0;
vis[point[x]] = 1;
Node a, b;
a.u = point[x], a.step = 0;
Q.push(a);
while(!Q.empty()){
a = Q.front();
Q.pop();
for(int i = 0; i < 2; i++){
int v = node[a.u].next[i];
if(vis[v]) continue;
vis[v] = 1;
if(node[v].cnt < (1 << 20)){
if(node[v].cnt) dis[x][node[v].is] = a.step + 1;
b.u = v, b.step = a.step + 1;
Q.push(b);
}
}
}
} void query(){
for(int i = 0; i < (1 << n); i++)
for(int j = 0; j < n; j++)
dp[i][j] = INF;
for(int i = 0; i < n; i++){
dp[node[point[i]].cnt][i] = length[i];
}
for(int i = 0; i < (1 << n); i++){
for(int j = 0; j < n; j++){
if(dp[i][j] == INF) continue;
for(int k = 0; k < n; k++){
int arr = node[point[k]].cnt;
// if(arr & i) continue;
dp[i | arr][k] = min(dp[i | arr][k], dp[i][j] + dis[j][k]);
}
}
}
int ans = INF;
for(int i = 0; i < n; i++){
ans = min(ans, dp[(1 << n) - 1][i]);
}
printf("%d\n", ans);
} }ac;
char s[1005];
int main(){
int ca = 1;
while(~scanf("%d%d", &n, &m) && n + m){
tot = 0;
ac.init();
for(int i = 0; i < n; i++){
scanf("%s", s);
ac.insert(s, i);
}
for(int i = 0; i < m; i++){
scanf("%s", s);
ac.insert(s, 20);
}
ac.build();
for(int i = 0; i < n; i++)
ac.bfs(i);
ac.query();
}
return 0;
}
/*
2 1
1110
0111
101
1 1
1
0 */

HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解的更多相关文章

  1. hdu 4057--Rescue the Rabbit(AC自动机+状压DP)

    题目链接 Problem Description Dr. X is a biologist, who likes rabbits very much and can do everything for ...

  2. HDU - 3247 Resource Archiver (AC自动机,状压dp)

    \(\quad\)Great! Your new software is almost finished! The only thing left to do is archiving all you ...

  3. HDU 3247 Resource Archiver (AC自动机+BFS+状压DP)

    题意:给定 n 个文本串,m个病毒串,文本串重叠部分可以合并,但合并后不能含有病毒串,问所有文本串合并后最短多长. 析:先把所有的文本串和病毒都插入到AC自动机上,不过标记不一样,可以给病毒标记-1, ...

  4. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  5. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. BZOJ1559 [JSOI2009]密码 【AC自动机 + 状压dp】

    题目链接 BZOJ1559 题解 考虑到这是一个包含子串的问题,而且子串非常少,我们考虑\(AC\)自动机上的状压\(dp\) 设\(f[i][j][s]\)表示长度为\(i\)的串,匹配到了\(AC ...

  7. zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)

    Time Limit: 10 Seconds      Memory Limit: 65536 KB Dr. X is a biologist, who likes rabbits very much ...

  8. hdu2825 Wireless Password(AC自动机+状压dp)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  9. HDU 3681 Prison Break(状压DP + BFS)题解

    题意:一张图,F是起点,Y是必须要到的点,D不能走,G可以充电.可以往四个方向走,每走一步花费一个电,走到G可以选择充满电或者不充,每个G只能充一次.问你走遍Y的最小初始点亮.number(G) + ...

随机推荐

  1. 使用npm install安装项目依赖的时候报错

    使用npm install安装项目依赖的时候报错: npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! node-sass@4.14.1 postin ...

  2. GlusterFS数据存储脑裂修复方案最全解析

    本文档介绍了glusterfs中可用于监视复制卷状态的heal info命令以及解决脑裂的方法 一. 概念解析 常见术语 名称 解释 Brick GlusterFS 的基本存储单元,由可信存储池中服务 ...

  3. 树莓派zero 使用usb串口连接

    使用minicom连接bash$ lsusbBus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hubBus 001 Device 0 ...

  4. celery 原理

    https://mp.weixin.qq.com/s/FzvZHQpF5mhV9t_HBzlcwg Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处 ...

  5. loj10087

    Southwestern Europe 2002,题面可参考 POJ 1201. 给定 n 个闭区间 [a_i,b_i] 和 n 个整数c_i .你需要构造一个整数集合Z ,使得对于任意i (1< ...

  6. 济南学校D1T3_hahaha

    [问题描述] 小Q对计算几何有着浓厚的兴趣.他经常对着平面直角坐标系发呆,思考一些有趣的问题.今天,他想到了一个十分有意思的题目: 首先,小Q会在轴正半轴和轴正半轴分别挑选个点.随后,他将轴的点与轴的 ...

  7. python 文件的方法

    1.open() 方法 Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError. 注意:使用 open ...

  8. MySql命令,吐血整理的Mysql,实话,真的吐血

    MySql命令,吐血整理的Mysql,实话,真的吐血 1.基本操作 2.数据库操作 3.表的操作 4.数据操作 5.字符集编码 6.数据类型(列类型) 7.列属性(列约束) 8.建表规范 9.SELE ...

  9. centos6.5 更新python2.6.6至2.7.6

    1.查看当前Python版本    # python -V      Python 2.6.6  2.下载Python2.7.6源码    # wget http://python.org/ftp/p ...

  10. rand()函数 不同区间 整数和浮点数

    C++中rand()范围[0,RAND_MAX] 1.生成整数随机数,a b均为整数,且    [a,b)---rand()%(b-a)+a [a,b]  ---rand()%(b-a+1)+a (a ...