给你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. MySQL数据库(6)_用户操作与权限管理、视图、存储过程、触发器、基本函数

    用户操作与权限管理 MySQL用户操作 创建用户 方法一: CREATE USER语句创建 CREATE USER "用户名"@"IP地址" IDENTIFIE ...

  2. bfc (收集的)

    一些基本概念 viewport: 展现网页的媒体,比如窗口或者某个区域,它的大小是有限制的,为了不被平台术语所束缚,我们给他起名viewport,中文意思就是视口. canvas: 而我们在渲染网页的 ...

  3. [转]u盘读不出来怎么办大汇总

    今天遇到的问题 http://www.upantool.com/jiaocheng/xiufu/2016/9958.html u盘读不出来怎么办大汇总 2016-12-14 21:42 来源: 本站整 ...

  4. 【HackerRank】Sherlock and MiniMax

    题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer  ...

  5. CMA内存管理子系统

    转:http://www.wowotech.net/memory_management/cma.html 前言 本文是近期学习CMA模块的一个学习笔记,方便日后遗忘的时候,回来查询以便迅速恢复上下文. ...

  6. git 使用教程 --基础一

    第一步:下载git   https://git-scm.com/ 第二步: 切到需要保存的文件夹下,执行: bogon:VBV mona$ git init #初始化,表示即将对当前文件夹进行版本控制 ...

  7. 最长公共子序列的C++实现---附二维指针的使用方法

    想了挺久到底第一篇在这儿的博客写什么好,刚好这两天又一次看到动态规划的LCS算法觉得还是有点意思的,就拿来写了写,第一篇博客就发它吧. #include<iostream> #includ ...

  8. Redis 高级实用特性

    1.安全性 设置客户端连接后进行任何其他操作前先验证密码. 因为Redis速度相当快,所以在一台比较好的服务器下,一个外部用户可以在一秒钟进行150K次的密码尝试,这意味着需要指定一个非常强大的密码来 ...

  9. Go sync模块

    // A WaitGroup waits for a collection of goroutines to finish.// The main goroutine calls Add to set ...

  10. C++逗号表达式

    c++中,逗号表达式的结果是最右边表达式的值