【HDU3247】 Resource Archiver(DP+AC自动机+最短路)
Resource Archiver
Time Limit: 10000MS Memory Limit: 100000KB 64bit IO Format: %I64d & %I64u Description
Great! Your new software is almost finished! The only thing left to do is archiving all your n resource files into a big one.
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.Input
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
For each test case, print the length of shortest string.Sample Input
2 21110011110110010 0Sample Output
5
【题意】
给出n个资源,m个病毒,将资源串拼接成一个串,必须包含所有的资源串,可以重叠,但是不能包含病毒。问最小的长度为多少。
【分析】
良心题解:http://blog.csdn.net/woshi250hua/article/details/8021283
· 一开始要想到DP,这是最简单的一步。而且n很小明显是状压,再用一维记录一下最后一个拼接的串。
即d[i][j]表示现在已经拼接的串集合为i,最后一个串为j的最小费用。
转移方程->d[i+(1<<k-1)][k]=min{d[i+(1<<k-1)][k],d[i][j]+cost[j][k]}
转移方程是很容易想的,重点就是求cost[j][k]。
暴力的话,目测是10*10*1000*1000,有点慢- -。
所以,要用到优美的性质才行。
把两种串都建在AC自动机里,打上不同标记,然后按上面所说的沿着next向下走。
理解了很久这个沿着next向下走的过程之后,我觉得意思应该是这样:
a串和b串连接有很多种方式,其实可以转换成 -> 自己生成一个串,使得前缀是a串,后缀是b串。(如上图第一种情况)
之前做生成串的题目都是在图上沿nest走的,这样只要不走到危险节点,就不会出现病毒串(fail的优美性质)。
所以,我们就可以利用这种方法求出拼接且不含危险串的串了。
要让这个拼接串长度最小,也就是走的从a的末尾节点走到b的末尾节点的路程最小。(因为答案为a串长度+走的步数,而a串长度一定)
就是一个最短路。
其实感觉有一个小小的BUG,就是无论a,b串怎么拼都含病毒串,他会输出一个大于l[a]+l[b]的数,相当于在a、b串中乱填东西,使得不含病毒串(如上图第二种情况)。这样就与“拼接”的含义不符了。
你可以简单的判断一下之类的,但是我没有判断也A了。可能是正确的最优解覆盖了它的原因。
AC自动机要建60000不是50000。(一开始RE了~~)
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 60010
#define Maxl 1200
#define Mn 20
#define INF 0xfffffff
#define Mod 20090717 int n,m;
char s[Maxl]; struct node
{
// int cnt;
int fail,mark;
int son[];
}t[Maxn];int tot;
// int num;
int p[Mn],sl[Mn]; void upd(int x)
{
// t[x].cnt=0;
t[x].mark=;
memset(t[x].son,,sizeof(t[x].son));
} int mymin(int x,int y) {return x<y?x:y;}
int mymax(int x,int y) {return x>y?x:y;} void read_trie(int tk)
{
scanf("%s",s+);
int len=strlen(s+);
if(tk!=-) sl[tk]=len;
int now=;
for(int i=;i<=len;i++)
{
int ind=s[i]-''+;
if(!t[now].son[ind])
{
t[now].son[ind]=++tot;
upd(tot);
}
now=t[now].son[ind];
if(i==len)
{
if(tk==-) t[now].mark=-;
else t[now].mark=tk,p[tk]=now;
}
}
} queue<int > q;
void build_AC()
{
while(!q.empty()) q.pop();
q.push();
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=;i<=;i++)
{
if(t[x].son[i])
{
t[t[x].son[i]].fail=x?t[t[x].fail].son[i]:;
q.push(t[x].son[i]);
}
else t[x].son[i]=t[t[x].fail].son[i];
}
// t[x].mark+=t[t[x].fail].mark;
}
} int dis[Mn][Maxn];
bool inq[Mn];
void spfa(int x)
{
while(!q.empty()) q.pop();
memset(dis[x],,sizeof(dis[x]));
memset(inq,,sizeof(inq));
dis[x][p[x]]=;
q.push(p[x]);inq[p[x]]=;
while(!q.empty())
{
int now=q.front();
for(int i=;i<=;i++) if(t[t[now].son[i]].mark!=-)
{
if(dis[x][t[now].son[i]]>dis[x][now]+)
{
dis[x][t[now].son[i]]=dis[x][now]+;
if(!inq[t[now].son[i]])
{
inq[t[now].son[i]]=;
q.push(t[now].son[i]);
}
}
}
q.pop();inq[now]=;
}
}
void init()
{
tot=;upd();
for(int i=;i<=n;i++)
{
read_trie(i);
}
for(int i=;i<=m;i++) read_trie(-);
build_AC();
for(int i=;i<=n;i++)
{
spfa(i);
}
} bool check(int x,int y,int z)
{
// if(x==0&&y==0&&z==0) return 1;
// if(y==0||z==0) return 0;
if(((<<y-)&z)==) return ;
int h=;
for(int i=;i<=n;i++)
{
if((<<i-)&z) h++;
}
if(h==x) return ;
return ;
} int f[Maxl][Mn];
void dp()
{
memset(f,,sizeof(f));
// f[0][0]=0;
for(int i=;i<=n;i++) f[<<i-][i]=sl[i];
for(int i=;i<n;i++)
for(int j=;j<=n;j++)
for(int k=;k<=(<<n)-;k++) if(check(i,j,k))
{
if(f[k][j]>INF) continue;
for(int l=;l<=n;l++) if((k&(<<l-))==)
f[k+(<<l-)][l]=mymin(f[k+(<<l-)][l],f[k][j]+dis[j][p[l]]);
}
int ans=INF;
for(int i=;i<=n;i++) ans=mymin(ans,f[(<<n)-][i]);
printf("%d\n",ans);
} int main()
{
int kase=;
while()
{
scanf("%d%d",&n,&m);
if(n==&&m==) break;
init();
dp();
}
return ;
}
[HDU3247]
2016-07-13 09:55:14
【HDU3247】 Resource Archiver(DP+AC自动机+最短路)的更多相关文章
- HDU3247 Resource Archiver(AC自动机+BFS+DP)
题目,求最短的包含所有n个DNA片段且不包含任何一个病毒片段的序列. 容易用所有DNA片段和病毒片段建一个AC自动机,构造fail时处理一下各个结点后缀是DNA或者病毒的情况,然后dp[S][u]表示 ...
- HDU-3247 Resource Archiver(AC自动机+BFS)
Description Great! Your new software is almost finished! The only thing left to do is archiving all ...
- HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解
题意:目标串n( <= 10)个,病毒串m( < 1000)个,问包含所有目标串无病毒串的最小长度 思路:貌似是个简单的状压DP + AC自动机,但是发现dp[1 << n][ ...
- HDU3247 Resource Archiver —— AC自动机 + BFS最短路 + 状压DP
题目链接:https://vjudge.net/problem/HDU-3247 Resource Archiver Time Limit: 20000/10000 MS (Java/Others) ...
- 咕咕(数位dp+AC自动机)
咕咕(数位dp+AC自动机) 若一个字符串的字符集合是0~m-1,那么称它为m进制字符串.给出n个m进制字符串\(s_i\),每个字符串的权值为\(v_i\).对于另一个m进制字符串\(S\),设\( ...
- 洛谷$P4045\ [JSOI2009]$密码 $dp$+$AC$自动机
正解:$dp$+$AC$自动机+搜索 解题报告: 传送门$QwQ$ 首先显然先建个$AC$自动机,然后考虑设$f_{i,j,k}$表示长度为$i$,现在在$AC$自动机的第$j$个位置,已经表示出来的 ...
- 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 ...
- HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...
- 【hdu3247-Resource Archiver】位压DP+AC自动机+SPFA
题意:给定n个文本串,m个病毒串,文本串重叠部分可以合并,但合并后不能含有病毒串,问所有文本串合并后最短多长. (2 <= n <= 10, 1 <= m <= 1000) 题 ...
随机推荐
- careercup-栈与队列 3.3
3.3 栈就像叠盘子,当盘子叠得太高时,就会倾斜倒下.因此,在真实的世界中,当一叠盘子 (栈)超过了一定的高度时,我们就会另起一堆,再从头叠起.实现数据结构SetOfStacks 来模拟这种情况.Se ...
- iOS开发-javaScript交互
前言 当前混合开发模式迎来了前所未有的发展,跨平台开发.热更新等优点决定了这种模式的重要地位.虽然前端界面在交互.动效等多方面距离原生应用还有差距,但毫无疑问混合开发只会被越来越多的公司接受.在iOS ...
- Bash中的$符号
脚本名称:$0 PID:$$ 参数个数:$# 脚本返回值:$? 第x个参数:$x 第10个以上的参数加大括号:${10} 所有参数:$@ #!/bin/bash echo "The prog ...
- 页面跳转 url地址的写法
跳转地址:分两类,wikipage和aspx页面: wikipage:当新建webpart,在网站里新建一个wikipage,然后将webpart添加进wikipage,这种情况下跳转页面需要添加si ...
- [IO] C# DirFileHelper文件与文件夹操作类教程与源码下载 (转载)
点击下载 DirFileHelper.zip 主要功能如下所示 // 摘要: // 向文本文件的尾部追加内容 // // 参数: // filePath: // 文件的绝对路径 // // conte ...
- Windows I/O模型、同步/异步、阻塞/非阻塞
转载自:http://www.cppblog.com/tx7do/articles/5954.html 同步 所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回.按照这个定义,其实 ...
- leetcode中一些要用到动态规划的题目
需要仔细回顾的题目: 1.Interleaving String 交叉存取字符串 2.Decode Ways 字符串解码 3.Subsets Subsets II 求一个 ...
- [学习笔记]设计模式之Command
为方便读者,本文已添加至索引: 设计模式 学习笔记索引 写在前面 在上篇Chain of Responsibility(职责链)模式笔记中,我们学习了一种行为型设计模式.今天,我们继续这一主题,来学习 ...
- gnome中文翻译之po
文件类型: po: 用msginit分析pot文件,生成各语言对应的po文件,比如中文的zh_CN.po. mo: 用msgfmt将po文件编译生成mo文件,这是二进制文件,不能直接编辑. gmo: ...
- GridView获取单个单元格的值
0.GridView中的所有数据都存储在Rows集合中,可以通过Rows的Cell属性获取单个单元格的值:如果某个单元格包含其他控件,则通过使用单元格的 Controls 集合,从单元格检索控件:如果 ...