HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
题目链接:Resource Archiver
解析:n个正常的串。m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少。
AC自己主动机 + bfs + 状态压缩DP
用最短路预处理出状态的转移。能够优化非常多
AC代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; const int INF = 0x3f3f3f3f;
struct Trie{
int next[60010][2], fail[60010], end[60010];
int root, L;
int newnode(){
for(int i=0; i<2; i++) next[L][i] = -1;
end[L++] = 0;
return L-1;
}
void init(){
L = 0;
root = newnode();
}
void insert(char buf[], int id){
int len = strlen(buf);
int now = root;
for(int i=0; i<len; i++){
if(next[now][buf[i] - '0'] == -1)
next[now][buf[i] - '0'] = newnode();
now = next[now][buf[i] - '0'];
}
end[now] = id;
}
void build(){
queue<int> Q;
fail[root] = root;
for(int i=0; i<2; i++)
if(next[root][i] == -1) next[root][i] = root;
else{
fail[ next[root][i] ] = root;
Q.push(next[root][i]);
}
while(!Q.empty()){
int now = Q.front();
Q.pop();
if(end[ fail[now] ] == -1) end[now] = -1;
else end[now] |= end[ fail[now] ];
for(int i=0; i<2; i++)
if(next[now][i] == -1) next[now][i] = next[ fail[now] ][i];
else{
fail[ next[now][i] ] = next[ fail[now] ][i];
Q.push(next[now][i]);
}
}
}
int g[11][11];
int dp[1025][11];
int cnt;
int pos[11];
int dis[60010]; void bfs(int k){
queue<int> q;
memset(dis, -1, sizeof(dis));
dis[pos[k]] = 0;
q.push(pos[k]);
while(!q.empty()){
int now = q.front();
q.pop();
for(int i=0; i<2; i++){
int tmp = next[now][i];
if(dis[tmp] < 0 && end[tmp] >= 0){
dis[tmp] = dis[now] + 1;
q.push(tmp);
}
}
}
for(int i=0; i<cnt; i++) g[k][i] = dis[pos[i]];
} int solve(int n){
pos[0] = 0;
cnt = 1;
for(int i=0; i<L; i++)
if(end[i] > 0) pos[cnt++] = i;
for(int i=0; i<cnt; i++) bfs(i); for(int i=0; i<(1<<n); i++)
for(int j=0; j<cnt; j++)
dp[i][j] = INF;
dp[0][0] = 0;
for(int i=0; i<(1<<n); i++)
for(int j=0; j<cnt; j++)
if(dp[i][j] < INF){
for(int k=0; k<cnt; k++){
if(g[j][k] < 0) continue;
if(j == k) continue;
dp[i | end[pos[k]]][k] = min(dp[i | end[pos[k]]][k], dp[i][j] + g[j][k]);
}
}
int ans = INF;
for(int i=0; i<cnt; i++)
ans = min(ans, dp[(1<<n)-1][i]);
return ans;
}
}; char buf[1010];
Trie ac; int main(){
#ifdef sxk
freopen("in.txt", "r", stdin);
#endif // sxk
int n, m;
while(scanf("%d%d", &n, &m) == 2){
if(n == 0 && m == 0) break;
ac.init();
for(int i=0; i<n; i++){
scanf("%s", buf);
ac.insert(buf, 1<<i);
}
for(int i=0; i<m; i++){
scanf("%s", buf);
ac.insert(buf, -1);
}
ac.build();
printf("%d\n", ac.solve(n));
}
return 0;
}
HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)的更多相关文章
- 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 ...
- POJ 3691 & HDU 2457 DNA repair (AC自己主动机,DP)
http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...
- HDU 2825 Wireless Password (AC自己主动机,DP)
pid=2825">http://acm.hdu.edu.cn/showproblem.php? pid=2825 Wireless Password Time Limit: 2000 ...
- HDU 2896 病毒侵袭 (AC自己主动机)
pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU 2896 病毒侵袭 AC自己主动机题解
本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...
- HDU - 2825 Wireless Password(AC自己主动机+DP)
Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless ne ...
- HDU 2222 Keywords Search AC自己主动机入门题
单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...
随机推荐
- JVM 垃圾回收器详解
小结: 新生代 串行Serial 并行 Parallel(关注吞吐量) 并行ParNew 老年代 串行 Serial Old 并行Para ...
- 二次封装OKHttp网络框架(1)
1. 框架功能简介:暂时只有get.post两个请求 2. 请求的主要流程和区别: 2.1 get请求: (1)创建请求客户的 OkHttpClient对象 (2)创建请求构建器 Request.Bu ...
- 9-4前端vue面试的问题
就没有什么顺序了,肖师傅的一些提问: 1- 配置文件中proxyTable的作用 2-@import '~styles/mixins.styl' ~的作用 3-vue模拟的本地中访问地址的url带有 ...
- (Entity framework 应用篇)把权限判断封装在数据库访问层
这里,我只是以一个例子,说一下简单权限控制,通过这个例子,大家可以设计庞大的权限管理层,把权限控制封装到数据库访问层,这样程序员就不用再写权限判断的代码了 首先,先看看我数据库DBContext的定义 ...
- BZOJ 2850: 巧克力王国 KDtree + 估价函数
Code: #include<bits/stdc++.h> #define maxn 100000 #define inf 1000000008 #define mid ((l+r)> ...
- Light Oj - 1134 Be Efficient
题目传送门:Be Efficient 题意:输入n和m,然后输入有n个元素的一个序列,问有多少个子序列元素的和能整除m. 思路:求前缀和,利用一个前缀的一个定理求解. 前缀和的一个定理是:每次求的前缀 ...
- 12. tie_breaker的使用原因和使用方法
主要知识点: tie_breaker的使用原因和使用方法 一.tie_breaker的使用原因 dis_max,只是取分数最高的那个query的分数而已,完全不考虑其他query的分数 ...
- 小白神器 - 一篇博客学会CSS
一. 简介 1. css定义 CSS是Cascading Style Sheets的简称,中文称为层叠样式表. 属性和属性值用冒号隔开,以分号结尾. 2. 四种引入方式 1.行内式 行内式是在标签 ...
- scrapy——7 scrapy-redis分布式爬虫,用药助手实战,Boss直聘实战,阿布云代理设置
scrapy——7 什么是scrapy-redis 怎么安装scrapy-redis scrapy-redis常用配置文件 scrapy-redis键名介绍 实战-利用scrapy-redis分布式爬 ...
- noip模拟赛 第k大区间
[问题描述] 定义一个长度为奇数的区间的值为其所包含的的元素的中位数. 现给出n个数,求将所有长度为奇数的区间的值排序后,第K大的值为多少. [输入] 输入文件名为kth.in. 第一行两个数n和k ...