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) + ...
随机推荐
- CF76A Gift
题目描述 有一个国家有N个城市和M条道路,这些道路可能连接相同的城市,也有可能两个城市之间有多条道路. 有一天,有一伙强盗占领了这个国家的所有的道路.他们要求国王献给他们礼物,进而根据礼物的多少而放弃 ...
- Spring-01-事务
Spring事务机制 spring事务机制最重要的两个配置项,隔离级别和传播特性. 1. 隔离级别 隔离级别针对高并发问题导致的数据库丢失更新问题 1.1 数据库的4大基本特征 原子性(Atomic) ...
- 新型赌博黑产攻击肆虐网吧: LOL博彩引流+棋牌盗号
https://mp.weixin.qq.com/s/BxnovV6jKqPkYfHEzjd_FA 新型赌博黑产攻击肆虐网吧: LOL博彩引流+棋牌盗号 看雪学院 2019-04-21
- 墓碑机制与 iOS 应用程序的生命周期
① 应用程序的状态 iOS 应用程序一共有 5 种状态: Not running:应用未运行 Inactive:应用运行在 foreground 但没有接收事件 Active:应用运行在 foregr ...
- WPF学习里程(二) XAML基础
1.什么是XAML? 官方语言: XAML是eXtensible Application Markup Language的英文缩写,相应的中文名称为可扩展应用程序标记语言,它是微软公司为构建应用程序用 ...
- IntelliJ Idea 解决 Could not autowire. No beans of 'xxxx' type found 的错误提示
IntelliJ Idea 解决 Could not autowire. No beans of 'xxxx' type found 的错误提示哈,在使用 @Autowired 时,今天又遇一坑,这俩 ...
- WeCenter (最新版) 前台RCE漏洞 (2020-02-22)
漏洞通过phar触发反序列化漏洞. 触发点:./models/account.php 中的 associate_remote_avatar 方法: 搜索全局调用了该方法的地方: ./app/accou ...
- 向数据库添加100W 条数据 性能测试
向数据库添加100W 条数据 性能测试 : 参考的相关网站目录: JDBC实现往MySQL插入百万级数据 https://www.cnblogs.com/fnz0/p/5713102.html MyS ...
- 一统江湖的大前端(10)——inversify.js控制反转
<大史住在大前端>前端技术博文集可在下列地址访问: [github总基地][博客园][华为云社区][掘金] 字节跳动幸福里大前端团队邀请各路高手前来玩耍,团队和谐有爱,技术硬核,字节范儿正 ...
- 零基础如何使用python处理字符串?
摘要:Python的普遍使用场景是自动化测试.爬取网页数据.科学分析之类,这其中都涉及到了对数据的处理,而数据的表现形式很多,今天我们来讲讲字符串的操作. 字符串是作为任意一门编程语言的基础,在P ...