HDU - 3247 Resource Archiver (AC自动机,状压dp)
\(\quad\)Great! Your new software is almost finished! The only thing left to do is archiving all your n resource files into a big one.
\(\quad\)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.
\(\quad\)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.
\(\quad\)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
\(\quad\)There will be at most 10 test cases, each begins with two integers in a single line: n and m (2 <= n <= 10, 1 <= m <= 1000). The next n lines contain the resources, one in each line. The next m lines contain the virus codes, one in each line. The resources and virus codes are all non-empty 01 strings without spaces inside. Each resource is at most 1000 characters long. The total length of all virus codes is at most 50000. The input ends with n = m = 0.
Output
\(\quad\)For each test case, print the length of shortest string.
Sample Input
2 2
1110
0111
101
1001
0 0
Sample Output
5
题意
\(\quad\)就是给你\(n\)个需要的串和\(m\)个病毒串,最后让你构造一个字符串,包含所有需要的串,不包括任何病毒串。
思路
\(\quad\)先讲\(n+m\)个串全部插入ac自动机中,然后去构造\(fail\)指针的时候注意将\(fail\)节点的信息传递给子节点。
\(\quad\)首先容易想到\(dp[i][j]\)表示ac自动机上状态为\(i\),包含需要串的状态为\(j\)时所需要的最少字符串数。但是数据范围\(i<=(略大于)5e4,j<=1024\),会\(MLE\)。
\(\quad\)观察数据范围可以发现\(n<<m\),那么说明,在ac自动机上,无用的节点占大多数,那么就可以找出全部有用的节点,用\(bfs\)求出所有有用节点两两之间的最小距离,然后直接在这些有用的点上跑\(dp\)就可以了,那么可以得到新的\(dp\)方程。
\(\quad\)\(dp[i][j]\)表示到有用节点\(i\),包含需要串的状态为\(j\)时所需要的最少字符长度。
\(\quad\)\(cnt[i]\)表示有用节点\(i\)上包含需要串的状态。
\(\quad\)\(cc[i][j]\)表示从有用节点\(i\)走到有用节点\(j\)需要的最少字符数。
\(\quad\)\(dp[k][j|cnt[k]] = min(dp[k][cnt[k]],dp[i][j]+cc[i][k])\)。
\(\quad\)最后在遍历一遍\(dp[i][mx]\),就可以得到答案。
/***************************************************************
> File Name : a.cpp
> Author : Jiaaaaaaaqi
> Created Time : 2019年04月29日 星期一 11时10分00秒
***************************************************************/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 6e4 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
char s[1010];
struct AC {
int node[maxn][2], fail[maxn], cnt[maxn], vir[maxn];
int root, sz;
int newnode() {
mes(node[++sz], 0);
cnt[sz] = vir[sz] = 0;
return sz;
}
void init() {
sz = 0;
root = newnode();
}
void insert(char *s, int f, int id) {
int len = strlen(s+1);
int rt = root;
for(int i=1; i<=len; i++) {
int k = s[i]-'0';
if(node[rt][k] == 0) {
node[rt][k] = newnode();
}
rt = node[rt][k];
}
if(f == 1) cnt[rt] |= (1<<(id-1));
else vir[rt] = 1;
}
void build() {
queue<int> q;
while(!q.empty()) q.pop();
fail[root] = root;
for(int i=0; i<=1; i++) {
if(node[root][i] == 0) {
node[root][i] = root;
} else {
fail[node[root][i]] = root;
q.push(node[root][i]);
}
}
while(!q.empty()) {
int u = q.front();
q.pop();
vir[u] |= vir[fail[u]];
cnt[u] |= cnt[fail[u]];
for(int i=0; i<=1; i++) {
if(node[u][i] == 0) {
node[u][i] = node[fail[u]][i];
} else {
fail[node[u][i]] = node[fail[u]][i];
q.push(node[u][i]);
}
}
}
}
int dis[maxn], point[500];
bool vis[maxn];
int cc[500][500], dp[500][1030];
void bfs(int st) {
queue<int> q;
while(!q.empty()) q.pop();
for(int i=1; i<=sz; i++) dis[i] = inf;
mes(vis, 0);
q.push(point[st]);
dis[point[st]] = 0;
vis[point[st]] = 1;
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i=0; i<=1; i++) {
int k = node[u][i];
if(vir[k]) continue;
if(vis[k]) continue;
dis[k] = dis[u]+1;
vis[k] = true;
q.push(k);
}
}
for(int i=1; i<=tol; i++) {
cc[st][i] = dis[point[i]];
}
}
void handle() {
tol = 0;
point[++tol] = 1;
for(int i=1; i<=sz; i++) {
if(cnt[i]) {
point[++tol] = i;
}
}
for(int i=1; i<=tol; i++) {
bfs(i);
}
// for(int i=1; i<=tol; i++) {
// for(int j=1; j<=tol; j++) {
// printf("%d%c", cc[i][j], j==tol ? '\n' : ' ');
// }
// }
// for(int i=1; i<=tol; i++) {
// printf("cnt[%d] = %d\n", i, cnt[point[i]]);
// }
}
int solve() {
int mx = (1<<n)-1;
for(int i=1; i<=tol; i++) {
for(int j=0; j<=mx; j++) {
dp[i][j] = inf;
}
}
dp[1][0] = 0;
for(int j=0; j<=mx; j++) {
for(int i=1; i<=tol; i++) {
if(dp[i][j] == inf) continue;
// printf("dp[%d][%d] = %d\n", i, j, dp[i][j]);
for(int k=1; k<=tol; k++) {
if(i == k) continue;
dp[k][j|cnt[point[k]]] = min(dp[k][j|cnt[point[k]]], dp[i][j]+cc[i][k]);
}
}
}
int ans = inf;
for(int i=1; i<=tol; i++) {
ans = min(ans, dp[i][mx]);
}
return ans;
}
} ac;
int main() {
while(scanf("%d%d", &n, &m), n||m) {
ac.init();
for(int i=1; i<=n; i++) {
scanf("%s", s+1);
ac.insert(s, 1, i);
}
for(int i=1; i<=m; i++) {
scanf("%s", s+1);
ac.insert(s, 2, i);
}
ac.build();
ac.handle();
int ans = ac.solve();
printf("%d\n", ans);
}
return 0;
}
HDU - 3247 Resource Archiver (AC自动机,状压dp)的更多相关文章
- 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自动机+BFS+状压DP)
题意:给定 n 个文本串,m个病毒串,文本串重叠部分可以合并,但合并后不能含有病毒串,问所有文本串合并后最短多长. 析:先把所有的文本串和病毒都插入到AC自动机上,不过标记不一样,可以给病毒标记-1, ...
- hdu 2825 aC自动机+状压dp
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解
题意:目标串n( <= 10)个,病毒串m( < 1000)个,问包含所有目标串无病毒串的最小长度 思路:貌似是个简单的状压DP + AC自动机,但是发现dp[1 << n][ ...
- 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 6086 -- Rikka with String(AC自动机 + 状压DP)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
- HDU 4057:Rescue the Rabbit(AC自动机+状压DP)***
http://acm.hdu.edu.cn/showproblem.php?pid=4057 题意:给出n个子串,串只包含‘A’,'C','G','T'四种字符,你现在需要构造出一个长度为l的串,如果 ...
随机推荐
- SpringBoot 2.0 mybatis mapper通用类
<!---mybatis通用类包含mybatis和连接池 mybatis和连接池就不需要引入--> <dependency> <groupId>tk.mybatis ...
- 《JavaScript高级程序设计》笔记:DOM2和DOM3(十二)
DOM1级主要定义的是HTML和XML文档的底层结构.DOM2级和DOM3级在这个结构基础上引入了更多的交互能力,也支持更高级的XML特性.为此DOM2级和DOM3级分为了很多的模块(模块直接具有某种 ...
- Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. To disable safe mode, toggle the option in Preferences -> SQL Editor and recon
数据库默认模式是主键不可进行修改操作,所以需要运行以下语句. SET SQL_SAFE_UPDATES = 0; -- 出现error1175使用.
- swing Jframe 界面风格
用法:在jframe里面 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel" ...
- Linux/Ubuntu 16.04 使用校园网客户端Dr.com DrClient 有线连网,同时开启WiFi热点
前面写过Ubuntu 16.04 使用校园网客户端 DrClient 无线上网,在这篇文章中将要介绍下,在Ubuntu 16.04上如何使用校园网客户端实现有线登录,这个问题也让博主困惑了很久,但是问 ...
- [RHEL 7]ISCSI服务端及客户端连接配置
环境RHEL7.4 1.搭建服务器端主机环境 网络配置 网卡eth0 10.0.0.1 网卡eth1 10.1.0.1 网卡eth2 10.2.0.1 网卡eth3 10.3.0.1 硬盘配置 添加一 ...
- Java基础系列--02_运算符和程序的语句
运算符: (1)算术运算符: +,-,*,/,%,++,--(加.减.乘.除.取余.自增,自减) ++和--的注意事项: a:他们的作用是自增或者自减 b:使用 1.单独使用 放在操作数据的前面和后面 ...
- MySQL慢查询&执行计划
参考文章: https://blog.csdn.net/tiantianw/article/details/53334566 http://www.cnblogs.com/luyucheng/p/62 ...
- Servlet开发笔记(一)
一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...
- Photoshop给草坪上的人物加上唯美的紫色霞光
最终效果 1.打开原图素材大图,创建可选颜色调整图层,对黄色,绿色进行调整,参数设置如图1,2,效果如图3.这一步给地面部分增加橙黄色. <图1> <图2> <图3> ...