HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解
题意:目标串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预处理)题解的更多相关文章
- hdu 4057--Rescue the Rabbit(AC自动机+状压DP)
题目链接 Problem Description Dr. X is a biologist, who likes rabbits very much and can do everything for ...
- HDU - 3247 Resource Archiver (AC自动机,状压dp)
\(\quad\)Great! Your new software is almost finished! The only thing left to do is archiving all you ...
- HDU 3247 Resource Archiver (AC自动机+BFS+状压DP)
题意:给定 n 个文本串,m个病毒串,文本串重叠部分可以合并,但合并后不能含有病毒串,问所有文本串合并后最短多长. 析:先把所有的文本串和病毒都插入到AC自动机上,不过标记不一样,可以给病毒标记-1, ...
- hdu 3247 AC自动+状压dp+bfs处理
Resource Archiver Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Ot ...
- hdu 2825 aC自动机+状压dp
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- BZOJ1559 [JSOI2009]密码 【AC自动机 + 状压dp】
题目链接 BZOJ1559 题解 考虑到这是一个包含子串的问题,而且子串非常少,我们考虑\(AC\)自动机上的状压\(dp\) 设\(f[i][j][s]\)表示长度为\(i\)的串,匹配到了\(AC ...
- zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)
Time Limit: 10 Seconds Memory Limit: 65536 KB Dr. X is a biologist, who likes rabbits very much ...
- hdu2825 Wireless Password(AC自动机+状压dp)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- HDU 3681 Prison Break(状压DP + BFS)题解
题意:一张图,F是起点,Y是必须要到的点,D不能走,G可以充电.可以往四个方向走,每走一步花费一个电,走到G可以选择充满电或者不充,每个G只能充一次.问你走遍Y的最小初始点亮.number(G) + ...
随机推荐
- Java运算符及包机制
Java中的运算符及包机制 算术运算符:+ - * / % ++ -- 赋值运算符:=,+=,-=,*=,/= 关系运算符:>,<,>=,<=,==,!=,instanceof ...
- jackson学习之三:常用API操作
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- (11)-Python3之--os模块
1.模块介绍 os模块是路径处理模块,它提供了多数操作系统的功能接口函数.当os模块被导入后,它会自适应于不同的操作系统平台,根据不同的平台进行相应的操作,在python编程时,经常和文件.目录打交道 ...
- day132:2RenMJ:MJ需求文档&MJ游戏流程&Egret白鹭引擎安装&TypeScript简要介绍
目录 1.麻将产品需求文档 2.麻将游戏流程 3.Egret白鹭引擎 4.TypeScript简要了解 5.TypeScript快速入门 1.麻将产品需求文档 1.麻将术语 1.名词术语 牌⼦: 序数 ...
- git的使用学习笔记---合并分支
一.使用场景 不同的分支需要合并 二.操作 1.首先要创建一个分支 git checkout -b mergedemo 创建文本 vim text.txt 添加文本 git add text.txt ...
- Monkey patching
"A monkey patch is a way to extend or modify the run-time code of dynamic languages without alt ...
- Service Locator Pattern 服务定位
https://www.geeksforgeeks.org/service-locator-pattern/ Service Locator Pattern Last Updated: 06-03-2 ...
- 我的刷题单(8/37)(dalao珂来享受切题的快感
P2324 [SCOI2005]骑士精神 CF724B Batch Sort CF460C Present CF482A Diverse Permutation CF425A Sereja and S ...
- LOJ10021 Addition Chains
题目描述 原题来自:ZOJ 1937 已知一个数列 A0,A1,A2,A3,...,Am(其中A0=1,Am=n,A0<A1<A2<A3<...<Am ).对于每个 k, ...
- ceph ---(ceph简介)
ceph简介: Ceph是一种为优秀的性能.可靠性和可扩展性而设计的统一的.分布式文件系统.ceph 的统一体现在可以提供文件系统.块存储和对象存储,分布式体现在可以动态扩展.在国内一些公司的云环境中 ...