题意:给定 n 个文本串,m个病毒串,文本串重叠部分可以合并,但合并后不能含有病毒串,问所有文本串合并后最短多长。

析:先把所有的文本串和病毒都插入到AC自动机上,不过标记不一样,可以给病毒标记-1,如果访问知道就知道不可行的,然后处理出两两串叠加的最小长度,这个要用bfs,在AC自动机上把这个处理出来,然后剩下的就是一个简单的DP了,dp[s][i] 表示状态为 s 时,i 串在后面,长度最短是多少。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
//#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int maxm = 1e6 + 5;
const int mod = 10007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
const int maxnode = 6e4 + 100;
const int sigma = 2;
int dist[15][15], last[15], cnt; struct Aho{
int ch[maxnode][sigma], f[maxnode];
int val[maxnode];
int sz; void clear(){ sz = 1; ms(ch[0], 0); }
inline int idx(char ch){ return ch - '0'; } void insert(char *s, int v){
int u = 0;
for(int i = 0; s[i]; ++i){
int c = idx(s[i]);
if(!ch[u][c]){
ms(ch[sz], 0);
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
if(v > 0) val[u] |= 1<<v;
else val[u] = v;
} void getFail(){
queue<int> q;
f[0] = 0;
for(int c = 0; c < sigma; ++c){
int u = ch[0][c];
if(u){ q.push(u); f[u] = 0; }
} while(!q.empty()){
int r = q.front(); q.pop();
for(int c = 0; c < sigma; ++c){
int u = ch[r][c];
if(!u){ ch[r][c] = ch[f[r]][c]; continue; }
q.push(u);
int v = f[r];
while(v && !ch[v][c]) v = f[v];
f[u] = ch[v][c];
if(val[u] > 0 && val[f[u]] > 0) val[u] |= val[f[u]];
}
}
} int d[maxnode]; void bfs(int s){
queue<int> q;
ms(d, INF); d[last[s]] = 0;
q.push(last[s]); while(!q.empty()){
int u = q.front(); q.pop();
for(int c = 0; c < sigma; ++c){
int nxt = ch[u][c];
if(d[nxt] > d[u] + 1 && val[nxt] >= 0){
d[nxt] = d[u] + 1;
q.push(nxt);
}
}
}
for(int i = 0; i < cnt; ++i)
dist[s][i] = d[last[i]];
}
}; Aho aho; char s[500006];
int dp[2100][13]; int main(){
while(scanf("%d %d", &n, &m) == 2 && n+m){
aho.cl;
for(int i = 1; i <= n; ++i){
scanf("%s", s);
aho.insert(s, i);
}
for(int i = 0; i < m; ++i){
scanf("%s", s);
aho.insert(s, -1);
}
aho.getFail();
cnt = 1;
for(int i = 0; i < aho.sz; ++i)
if(aho.val[i] > 0) last[cnt++] = i;
for(int i = 0; i < cnt; ++i) aho.bfs(i);
ms(dp, INF);
dp[1][0] = 0;
int all = 1<<cnt;
FOR(i, 0, all) for(int j = 0; j < cnt; ++j){
if(dp[i][j] == INF) continue;
for(int k = 0; k < cnt; ++k){
if(i&1<<k) continue;
dp[i|1<<k][k] = min(dp[i|1<<k][k], dp[i][j] + dist[j][k]);
}
}
int ans = INF;
for(int i = 0; i < cnt; ++i)
ans = min(ans, dp[all-1][i]);
printf("%d\n", ans);
}
return 0;
}

  

HDU 3247 Resource Archiver (AC自动机+BFS+状压DP)的更多相关文章

  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 ...

  2. HDU3247 Resource Archiver (AC自动机+spfa+状压DP)

    Great! Your new software is almost finished! The only thing left to do is archiving all your n resou ...

  3. HDU3247 Resource Archiver —— AC自动机 + BFS最短路 + 状压DP

    题目链接:https://vjudge.net/problem/HDU-3247 Resource Archiver Time Limit: 20000/10000 MS (Java/Others)  ...

  4. Resource Archiver HDU - 3247 AC自动机+BFS+状压

    题意: 给出n个资源串,m个病毒串,现在要如何连接资源串使得不含病毒串(可以重叠,样例就是重叠的). 题解: 这题的套路和之前的很不同了,之前的AC自动机+DP的题目一般都是通过teir图去转移, 这 ...

  5. Walk Through Squares HDU - 4758 AC自动机+简单状压DP

    题意:给你两个串,求用m个R,n个D能组成多少个包含这两个串 题解:先构造一个AC自动机记录每个状态包含两个串的状态, 状态很容易定义 dp[i][j][k][status]表示在AC自动机K这个节点 ...

  6. HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解

    题意:目标串n( <= 10)个,病毒串m( < 1000)个,问包含所有目标串无病毒串的最小长度 思路:貌似是个简单的状压DP + AC自动机,但是发现dp[1 << n][ ...

  7. hdu 4856 Tunnels (bfs + 状压dp)

    题目链接 The input contains mutiple testcases. Please process till EOF.For each testcase, the first line ...

  8. HDU 3920Clear All of Them I(状压DP)

    HDU 3920   Clear All of Them I 题目是说有2n个敌人,现在可以发n枚炮弹,每枚炮弹可以(可以且仅可以)打两个敌人,每一枚炮弹的花费等于它所行进的距离,现在要消灭所有的敌人 ...

  9. HDU-4856 Tunnels (BFS+状压DP)

    Problem Description Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In hi ...

随机推荐

  1. as3 加载库声音报错

    排除法:(依次排序,从简单到难) 1.引用的声音类名与声音链接名字是否一致,可trace声音对象字符串检验 2.引用的声音对象是否不存在 ,可trace声音对象检验 3.最后检验是否当前swf中,其中 ...

  2. c++变量声明、定义,const变量

    变量声明和定义的主要区别: 声明不分配存储空间,定义分配存储空间. 变量可以声明多次,但只能定义一次(一个变量只能在一个源文件中定义) 声明通常放在头文件(.h)中,定义放在源文件(.cpp)中 变量 ...

  3. requireJS-初识

    浅谈requireJS 2016-04-26 21:44 by 猴子猿, 726 阅读, 0 评论, 收藏, 编辑 项目中大都使用模块化开发,requireJS作为AMD模块开发的典范,所以有必要学习 ...

  4. java是如何编码解码的

    在上篇博客中LZ阐述了java各个渠道转码的过程,阐述了java在运行过程中那些步骤在进行转码,在这些转码过程中如果一处出现问题就很有可能会产生乱码!下面LZ就讲述java在转码过程中是如何来进行编码 ...

  5. inotify用法简介及结合rsync实现主机间的文件实时同步

    一.inotify简介 inotify是Linux内核2.6.13 (June 18, 2005)版本新增的一个子系统(API),它提供了一种监控文件系统(基于inode的)事件的机制,可以监控文件系 ...

  6. 问题解决Android studio遇到 java.lang.OutOfMemoryError: GC app:transformClassesWithDexForDebug解决方法 以及gradle优化

    http://blog.csdn.net/xiaoxing0828/article/details/52242090

  7. mysql mapper中增删查改

    //1.增 public int insert(Port port) ; //2.删 public int deleteM(String id);//3.改 public int update(Por ...

  8. 使用UUID方法生成全球唯一标识

    需要生成唯一字符串,如生成应用标识等,可以直接用java.util.UUID类实现. UUID(Universally Unique Identifier)全局唯一标识符,是指在一台机器上生成的数字, ...

  9. 4.n的高精度阶乘---优化

    题目:对于每组测试数据,在一行中给出一非负整数n(n小于等于100) 样例输入 3 5 10 样例输出 6 120 3628800 超时的代码如下:#include <iostream># ...

  10. 解决安装Apache中出现checking for APR... no configure: error: APR not found. Please read the documentation的问题

    Linux中安装Apache 编译出现问题: 解决办法: 1.下载所需要的软件包 wget http://archive.apache.org/dist/apr/apr-1.4.5.tar.gz wg ...