给你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)的更多相关文章

  1. HDU 2825 Wireless Password(AC自动机 + 状压DP)题解

    题意:m个密码串,问你长度为n的至少含有k个不同密码串的密码有几个 思路:状压一下,在build的时候处理fail的时候要用 | 把所有的后缀都加上. 代码: #include<cmath> ...

  2. HDU2825 Wireless Password(AC自动机+状压DP)

    题目问长度n至少包含k个咒语的字符串有多少个.也是比较入门的题.. dp[i][j][S]表示长度i(在自动机上转移k步)且后缀状态为自动机上第j个结点且当前包含咒语集合为S的方案数 dp[0][0] ...

  3. HDU - 2825 Wireless Password (AC自动机+状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2825 题意:给一些字符串,构造出长度为n的字符串,它至少包含k个所给字符串,求能构造出的个数. 题解: ...

  4. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. hdu2825 Wireless Password(AC自动机+状压dp)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  6. BZOJ1559 [JSOI2009]密码 【AC自动机 + 状压dp】

    题目链接 BZOJ1559 题解 考虑到这是一个包含子串的问题,而且子串非常少,我们考虑\(AC\)自动机上的状压\(dp\) 设\(f[i][j][s]\)表示长度为\(i\)的串,匹配到了\(AC ...

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

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

  8. zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)

    Time Limit: 10 Seconds      Memory Limit: 65536 KB Dr. X is a biologist, who likes rabbits very much ...

  9. hdu 4057--Rescue the Rabbit(AC自动机+状压DP)

    题目链接 Problem Description Dr. X is a biologist, who likes rabbits very much and can do everything for ...

随机推荐

  1. python常用模块——time模块

    参考博客:http://blog.csdn.net/SeeTheWorld518/article/details/48314501 http://www.jb51.net/article/49325. ...

  2. django 操作前端数据

    django 利用json处理前端页面数据,FLASK当中也同样   def create_company(request):if request.user.is_superuser:custom_l ...

  3. Python基础(11)_python模块之time模块、rando模块、hashlib、os模块

    一.模块 1.什么是模块:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀 模块的本质:模块的本质是一个py文件 2.模块分为三类:1)内置模块:2)第三方模块: ...

  4. 【Tech】POI标签分类

    寒假老板给的任务,让我重现这个实验http://www.liuhaihua.cn/archives/15565.html.自己就随便试了下,用的都是比较经典(lao)的算法和知识,记录一下. 一.从网 ...

  5. 网络:W5500 UDP数据包格式注意事项

     1. 主题 使用W5500测试UDP功能,发现收到的数据包和wireshark抓包的数据不同. 原来W5500接收寄存器的数据包并不是网络上的数据流,而是经过内部处理后展现出来的. 对于这个问题目前 ...

  6. level-4

    [1.网页乱码的问题是如何产生的.怎么样解决?] 造成html网页乱码原因主要是html源代码内中文字内容与html编码不同造成.主要情况有以下三种: 1.比如网页源代码是gbk的编码,而内容中的中文 ...

  7. python__Django 分页

    自定义分页的类: #!/usr/bin/env python # -*- coding: utf-8 -*- # Created by Mona on 2017/9/20 from django.ut ...

  8. php自带函数去除html标记

    strip_tags 去掉 HTML 及 PHP 的标记. 语法: string strip_tags(string str); 传回值: 字串 函式种类: 资料处理 内容说明 本函式可去掉字串中包含 ...

  9. gem Errno::ECONNRESET: Connection reset by peer - SSL_connect

    问题描述 在使用gem安装软件包时,会时常遇到下面的问题: ERROR: While executing gem ... (Gem::RemoteFetcher::FetchError) Errno: ...

  10. explicit c++

    C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造函数是显示的, 而非隐式的, 跟它相对应的另一个关键字是implicit, 意思是隐藏的,类构造函数默认情况 ...