hdu 3247 AC自动+状压dp+bfs处理
Resource Archiver
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 2382 Accepted Submission(s): 750
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.
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.
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.
/*
hdu 3247 AC自动+状压dp+bfs处理 给你n个正常子串,m个病毒子串,求出最短的字符串(包含所有正常子串,不包含病毒串) 因为正常子串只有十个,所以考虑二进制来记录。
即dp[i][j]表示 包含的正常串的状态为i,最后一步的状态为j的最短情况.
然后试了下发现超内存 卒~ 然后膜拜大神,发现我们可以预处理出来正常串之间的最短距离. 像这样我们只需要枚举所有的
正常串. 而我原先那个思路需要枚举所有的节点总共需要dp[1<<10][60040]. 而对于通过bfs优化后
我们只需要枚举正常串 最多有11个 -> dp[1<<10][11]. hhh-2016-04-30 14:34:51
*/
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <functional>
#include <map>
using namespace std;
#define lson (i<<1)
#define rson ((i<<1)|1)
typedef unsigned long long ll;
typedef unsigned int ul;
const int mod = 20090717;
const int INF = 0x3f3f3f3f;
const int N = 100050;
int cnt;
int n,m;
int dp[1<<10][205];
int G[205][205];
int pos[205];
struct Tire
{
int nex[N][2],fail[N],ed[N]; int root,L;
int newnode()
{
for(int i = 0; i < 2; i++)
nex[L][i] = -1;
ed[L++] = 0;
return L-1;
} void ini()
{
L = 0,root = newnode();
} void inser(char buf[],int val)
{
int len = strlen(buf);
int now = root;
for(int i = 0; i < len; i++)
{
int ta = buf[i]-'0';
if(nex[now][ta] == -1)
nex[now][ta] = newnode();
now = nex[now][ta];
}
if(val < 0)
ed[now] = val;
else
ed[now] = (1<<val);
} void build()
{
queue<int >q;
fail[root] = root;
for(int i = 0; i < 2; i++)
if(nex[root][i] == -1)
nex[root][i] = root;
else
{
fail[nex[root][i]] = root;
q.push(nex[root][i]);
}
while(!q.empty())
{
int now = q.front();
q.pop();
if(ed[fail[now]] < 0)
ed[now] = ed[fail[now]];
else if(ed[now] == 0)
ed[now] = ed[fail[now]];
for(int i = 0; i < 2; i++)
{
if(nex[now][i] == -1)
nex[now][i] = nex[fail[now]][i];
else
{
fail[nex[now][i]] = nex[fail[now]][i];
q.push(nex[now][i]);
}
}
}
}
int dis[N];
void Path(int k)
{
int now;
queue<int >q;
q.push(pos[k]);
memset(dis,-1,sizeof(dis));
dis[pos[k]] = 0;
while(!q.empty())
{
now = q.front();
q.pop();
for(int i =0;i < 2;i++)
{
int t = nex[now][i];
if(dis[t] < 0 && ed[t] >= 0)
{
dis[t] = dis[now]+1;
q.push(t);
}
}
}
for(int i = 0;i < cnt;i++)
{
G[k][i] = dis[pos[i]];
}
} int Min(int a,int b)
{
if(a < 0)
return b;
else if(b < 0)
return a;
else
return min(a,b);
} void solve()
{
memset(dp,-1,sizeof(dp));
dp[0][0] = 0; for(int i = 0;i < (1<<n);i++)
{
for(int j = 0;j < cnt;j++)
{
if(dp[i][j] < 0)
continue;
for(int k = 0;k < cnt;k++)
{
if(G[j][k] < 0)
continue;
int t = (i|ed[pos[k]]);
dp[t][k] = Min(dp[i][j] + G[j][k],dp[t][k]);
}
}
}
int ans = -1;
for(int i = 0;i < cnt;i++)
{
ans = Min(ans,dp[(1<<n)-1][i]);
}
cout << ans <<"\n";
}
}; Tire ac;
char buf[N];
int main()
{
while(scanf("%d%d",&n,&m)==2 && n && m)
{
ac.ini();
for(int i = 0; i < n; i++)
{
scanf("%s",buf);
ac.inser(buf,i);
}
for(int i =0 ; i < m; i++)
{
scanf("%s",buf);
ac.inser(buf,-1);
}
ac.build();
pos[0] = 0;
cnt = 1;
for(int i = 0; i < ac.L; i++)
{
if(ac.ed[i] > 0)
pos[cnt++] = i;
}
memset(G,-1,sizeof(G));
for(int i = 0; i < cnt; i++)
ac.Path(i);
ac.solve();
}
return 0;
}
hdu 3247 AC自动+状压dp+bfs处理的更多相关文章
- 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][ ...
- BZOJ_3049_[Usaco2013 Jan]Island Travels _状压DP+BFS
BZOJ_3049_[Usaco2013 Jan]Island Travels _状压DP+BFS Description Farmer John has taken the cows to a va ...
- HDU 3681 Prison Break(状压DP + BFS)题解
题意:一张图,F是起点,Y是必须要到的点,D不能走,G可以充电.可以往四个方向走,每走一步花费一个电,走到G可以选择充满电或者不充,每个G只能充一次.问你走遍Y的最小初始点亮.number(G) + ...
- BZOJ1559 [JSOI2009]密码 【AC自动机 + 状压dp】
题目链接 BZOJ1559 题解 考虑到这是一个包含子串的问题,而且子串非常少,我们考虑\(AC\)自动机上的状压\(dp\) 设\(f[i][j][s]\)表示长度为\(i\)的串,匹配到了\(AC ...
- HDU 5765 Bonds(状压DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...
- 计蒜客 30994 - AC Challenge - [状压DP][2018ICPC南京网络预赛E题]
题目链接:https://nanti.jisuanke.com/t/30994 样例输入: 5 5 6 0 4 5 1 1 3 4 1 2 2 3 1 3 1 2 1 4 样例输出: 55 样例输入: ...
- HDU 3001 Travelling (状压DP,3进制)
题意: 给出n<=10个点,有m条边的无向图.问:可以从任意点出发,至多经过同一个点2次,遍历所有点的最小费用? 思路: 本题就是要卡你的内存,由于至多可经过同一个点2次,所以只能用3进制来表示 ...
- hdu 4778 Gems Fight! 状压dp
转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...
随机推荐
- Linux 复习
shift + control + + 终端窗口放大 control + - 终端窗口缩小 ls -alh > laowang.txt 重定向,并覆盖源文件内容 ls -alh >& ...
- 用greenlet实现Python中的并发
from greenlet import greenlet def test1(): print 12 gr2.switch() print 34 def test2(): print 56 gr1. ...
- 学习less
什么是less?LESSCSS是一种动态样式语言,属于CSS预处理语言的一种,它使用类似CSS的语法,为CSS的赋予了动态语言的特性,如变量.继承.运算.函数等,更方便CSS的编写和维护. less哪 ...
- kali rolling更新源之gpg和dirmngr问题
1.编辑 /etc/apt/source.list gedit /etc/apt/sources.list 输入更新源,可以选任何可用更新源,这里设置官方源 deb http://http.kali. ...
- nodeJS基于smtp发邮件
邮件的协议smtp是tcp/ip族中的一个协议,所以我们这次考虑使用net模块来发送邮件. const net = require('net') const assert = require('ass ...
- CentOS 7 使用yum安装出现错误
CentOS 7 使用yum安装软件出现错误: Loaded plugins: fastestmirror 此错误信息可能是因为DNS配置错误,可以通过更改配置文件来解决: 1. 使用vi打开DNS的 ...
- 启动eclipse时出现“Failed to load the JNI shared library jvm.dll”错误及解决
昨晚安装另一个版本的eclipse,启动时出现了"Failed to load the JNI shared library jvm.dll"错误: 1.刚开始以为是因为当时没有将 ...
- ELK学习总结(4-2)关于导入数据
用REST API的_bulk来批量插入,可以达到5到10w条每秒 把数据写进json文件,然后再通过批处理,执行文件插入数据: 1.先定义一定格式的json文件,文件不能过大,过大会报错 2.后用c ...
- Django REST framework+Vue 打造生鲜超市(四)
五.商品列表页 5.1.django的view实现商品列表页 (1)goods/view_base.py 在goods文件夹下面新建view_base.py,为了区分django和django res ...
- [SHOI2009] 会场预约 - Treap
Description PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也 ...