UVALive - 4126 Password Suspects (AC自动机+状压dp)
给你m个字符串,让你构造一个字符串,包含所有的m个子串,问有多少种构造方法。如果答案不超过42,则按字典序输出所有可行解。
由于m很小,所以可以考虑状压。
首先对全部m个子串构造出AC自动机,每个节点有一个附加属性val[u]代表结点u包含的子串集合。
设dp[l][S][u]为长度为l,包含子串集合为S,当前在结点u时,接下来能构造出的合法字符串总数,则dp[l][S][u]=∑dp[l+1][S|val[v]][v],v=go[u][i],0<=i<26;
两遍dfs,一遍dp,一遍输出答案即可。
注意一个结点可能是多个字符串的结尾,因此在建AC自动机的时候,每个结点的val值还要加上其pre结点的val值。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=+;
int n,m,ka;
char s[N]; struct AC {
static const int N=+,M=;
int go[N][M],pre[N],tot,val[N];
ll d[][(<<)+][N],ans;
int idx(char ch) {return ch-'a';}
void init() {tot=; newnode();}
int newnode() {
int u=tot++;
memset(go[u],,sizeof go[u]);
val[u]=pre[u]=;
return u;
}
void ins(char* s,int x) {
int u=,n=strlen(s);
for(int i=; i<n; u=go[u][idx(s[i])],++i)
if(!go[u][idx(s[i])])go[u][idx(s[i])]=newnode();
val[u]|=<<x;
}
void build() {
queue<int> q;
for(int i=; i<M; ++i)if(go[][i])q.push(go[][i]);
while(!q.empty()) {
int u=q.front();
q.pop();
val[u]|=val[pre[u]];
for(int i=; i<M; ++i) {
if(go[u][i]) {
pre[go[u][i]]=go[pre[u]][i];
q.push(go[u][i]);
} else go[u][i]=go[pre[u]][i];
}
}
}
ll dp(int l,int S,int u) {
if(l==n)return S==(<<m)-?:;
ll& ret=d[l][S][u];
if(~ret)return ret;
ret=;
for(int i=; i<M; ++i) {
int v=go[u][i];
ret+=dp(l+,S|val[v],v);
}
return ret;
}
void dfs(int l,int S,int u) {
if(l==n) {
if(S==(<<m)-)s[l]='\0',puts(s);
return;
}
for(int i=; i<M; ++i) {
int v=go[u][i];
if(d[l+][S|val[v]][v])s[l]=i+'a',dfs(l+,S|val[v],v);
}
}
void run() {
memset(d,-,sizeof d);
ans=dp(,,);
printf("%lld suspects\n",ans);
if(ans<=)dfs(,,);
}
} ac; int main() {
while(scanf("%d%d",&n,&m)&&n) {
printf("Case %d: ",++ka);
ac.init();
for(int i=; i<m; ++i) {
scanf("%s",s);
ac.ins(s,i);
}
ac.build();
ac.run();
}
return ;
}
UVALive - 4126 Password Suspects (AC自动机+状压dp)的更多相关文章
- HDU 2825 Wireless Password(AC自动机 + 状压DP)题解
题意:m个密码串,问你长度为n的至少含有k个不同密码串的密码有几个 思路:状压一下,在build的时候处理fail的时候要用 | 把所有的后缀都加上. 代码: #include<cmath> ...
- HDU2825 Wireless Password(AC自动机+状压DP)
题目问长度n至少包含k个咒语的字符串有多少个.也是比较入门的题.. dp[i][j][S]表示长度i(在自动机上转移k步)且后缀状态为自动机上第j个结点且当前包含咒语集合为S的方案数 dp[0][0] ...
- HDU - 2825 Wireless Password (AC自动机+状压DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2825 题意:给一些字符串,构造出长度为n的字符串,它至少包含k个所给字符串,求能构造出的个数. 题解: ...
- hdu 2825 aC自动机+状压dp
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu2825 Wireless Password(AC自动机+状压dp)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- BZOJ1559 [JSOI2009]密码 【AC自动机 + 状压dp】
题目链接 BZOJ1559 题解 考虑到这是一个包含子串的问题,而且子串非常少,我们考虑\(AC\)自动机上的状压\(dp\) 设\(f[i][j][s]\)表示长度为\(i\)的串,匹配到了\(AC ...
- HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解
题意:目标串n( <= 10)个,病毒串m( < 1000)个,问包含所有目标串无病毒串的最小长度 思路:貌似是个简单的状压DP + AC自动机,但是发现dp[1 << n][ ...
- zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)
Time Limit: 10 Seconds Memory Limit: 65536 KB Dr. X is a biologist, who likes rabbits very much ...
- hdu 4057--Rescue the Rabbit(AC自动机+状压DP)
题目链接 Problem Description Dr. X is a biologist, who likes rabbits very much and can do everything for ...
随机推荐
- JAVA学习笔记----【转】 java.toString() ,(String),String.valueOf的区别
在java项目的实际开发和应用中,常常需要用到将对象转为String这一基本功能.本文将对常用的转换方法进行一个总结. 常用的方法有Object#toString(),(String)要转换的对象,S ...
- IM协议
四种协议英文全称与简称 1->IMPP(Instant Messaging And PresenceProtocol):即时信息和空间协议 2->PRIM(Presence and Ins ...
- time函数计算时间
学过C语言的都知道有个time函数可以计算时间, 也好像知道time(NULL)返回的是一个距离1970年1月1日0时0分0秒的秒数. #include <stdio.h> #includ ...
- 每天一个Linux命令(48)ping命令
ping命令用来测试主机之间网络的连通性. (1)用法: 用法: ping [参数] [主机名或IP地址] (2)功能: 功能: 确定网络和各外部主机的状态 ...
- Linux基本命令 vim命令(一)
vim的三种工作模式 命令模式.输入模式和编辑模式的相互转换,如图 命令模式:使用 Vim 编辑文件时,默认处于命令模式.在此模式下,可以使用上.下.左.右键或者 k.j.h.l 命令进行光标移动,还 ...
- JSON.parse和JSON.stringify的作用
//JSON.parse将字符串格式json转化为json对象 var str='{"name":"lingling","age":&quo ...
- Android系统--Binder系统具体框架分析(一)补充
Android系统--Binder系统具体框架分析(一)补充 补充:对Binder驱动分析一的代码补充,添加saygoobye和saygoodbye_to服务 test_server.h #ifnde ...
- VMware Big Data Extensions 安装步骤
文档地址:https://pubs.vmware.com/bde-2/index.jsp 第一步,部署BigDataExtensions OVF模板,并登陆Console修改默认密码 第二步,在 Se ...
- poj 1679 The Unique MST 【次小生成树+100的小数据量】
题目地址:http://poj.org/problem?id=1679 2 3 3 1 2 1 2 3 2 3 1 3 4 4 1 2 2 2 3 2 3 4 2 4 1 2 Sample Outpu ...
- EntityFramework 学习 一 Colored Entity in Entity Framework 5.0
You can change the color of an entity in the designer so that it would be easy to see related groups ...