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 ...
随机推荐
- hihocode 编程练习赛17
1. f1 score 首先了解f1 score的计算方法, 我记得是学信息检索知道的, 然后简单处理就行. 由于我写的比较麻烦, 中间处理过程引入了一些除数为0的情况,导致错了很多次.其实是很简单的 ...
- Offer收割_4
1.水题 2.BFS宽搜(使用优先队列priority_queue) 4.题意:给数组a.要求重排列数组,使得数组中的任意相邻的两个元素不同.如果存在多个方案,那么选择字典序最小的方案. 如果不能满 ...
- 【转】Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例
概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...
- 前端开发之旅- 移动端HTML5实现文件上传
一. 在一个客户的webapp项目中需要用到 html5调用手机摄像头,找了很多资料,大都是 js调用api 然后怎样怎样,做了几个demo测试发现根本不行, 后来恍然大悟,用html5自带的 in ...
- (三)Python 学习第三天--GUI桌面项目
(代码参考了别人的代码,只做学习用途!!!最近因为写论文,好久没有记录,好内疚...今天学习了一个小案例,做一下) 主要使用模块:tkinter 代码如下: from tkinter import * ...
- 【sqli-labs】 less46 GET -Error based -Numeric -Order By Clause(GET型基于错误的数字型Order By从句注入)
http://192.168.136.128/sqli-labs-master/Less-46/?sort=1 sort=4时出现报错 说明参数是添加在order by 之后 错误信息没有屏蔽,直接使 ...
- C# 获得枚举值中所有数据到Array(数组)中
Array LogType = Enum.GetValues(LogTypes.登录.GetType()); public enum LogTypes { 登录, 添加, 修改, 删除, 导出, 异常 ...
- CAD动态绘制带面积周长的圆(com接口)
CAD绘制图像的过程中,画圆的情况是非常常见的,用户可以在控件视区点取任意一点做为圆心,再动态点取半径绘制圆. 主要用到函数说明: _DMxDrawX::DrawCircle 绘制一个圆.详细说明如下 ...
- Python3爬取前程无忧数据分析工作并存储到MySQL
1.导入包import requests #取数from lxml import etree #用xpath解析import pymysql #连接数据库import chardet #自动获取编码2 ...
- c# winform中使用WebKit实现网页与winform的交互
1.工作 一年多了,一直没对自己在工作遇到的问题进行总结,每次遇到问题都要在网上找资料,导致完成项目之后,时间久了就会生疏.所以下定 决定总结自己在工作中遇到的各种问题. 进入正题:第一次写还请大神多 ...