HDU2825 Wireless Password


Problem Description

Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless network in the building. Liyuan did not know the password of the network, but he got some important information from his neighbor. He knew the password consists only of lowercase letters ‘a’-‘z’, and he knew the length of the password. Furthermore, he got a magic word set, and his neighbor told him that the password included at least k words of the magic word set (the k words in the password possibly overlapping).

For instance, say that you know that the password is 3 characters long, and the magic word set includes ‘she’ and ‘he’. Then the possible password is only ‘she’.

Liyuan wants to know whether the information is enough to reduce the number of possible passwords. To answer this, please help him write a program that determines the number of possible passwords.

Input

There will be several data sets. Each data set will begin with a line with three integers n m k. n is the length of the password (1<=n<=25), m is the number of the words in the magic word set(0<=m<=10), and the number k denotes that the password included at least k words of the magic set. This is followed by m lines, each containing a word of the magic set, each word consists of between 1 and 10 lowercase letters ‘a’-‘z’. End of input will be marked by a line with n=0 m=0 k=0, which should not be processed.

Output

For each test case, please output the number of possible passwords MOD 20090717.

Sample Input

10 2 2

hello

world

4 1 1

icpc

10 0 0

0 0 0

Sample Output

2

1

14195065


我觉得挺好的一道题。。一开始只知道是DP,然后写了一个循环,后面发现不能重复计算,然后改成了状压DP,改成了队列实现DP,上线是步数小于等于长度,然后每一次DP转移将步数加1就好了,然后或一下当前存在的状态。

处理fail指针的时候做两个小优化,一个是在处理的时候就把fail链上的id全部统计起来,二个是把失配后跳到的节点直接储存成儿子节点

然后注意开一下取模的操作,防止TLE


#include<bits/stdc++.h>
using namespace std;
#define N 110
#define Mod 20090717
struct Node{
int fail,id,ch[26];
void clean(){
fail=id=0;
memset(ch,0,sizeof(ch));
}
}t[N];
struct Node1{int id,tmp,now;};
int n,m,p,tot=0;
int dp[N][26][1025],c[1025];
bool vis[N][26][1025];
char s[20][N];
void init(){
tot=1;
t[0].clean();t[1].clean();
for(int i=0;i<26;i++)t[0].ch[i]=1;
memset(dp,0,sizeof(dp));
}
int index(char c){return c-'a';}
void insert(char *s,int id){
int len=strlen(s),u=1;
for(int i=0;i<len;i++){
int tmp=index(s[i]);
if(!t[u].ch[tmp])
t[t[u].ch[tmp]=++tot].clean();
u=t[u].ch[tmp];
}
t[u].id=1<<(id-1);
}
void buildFail(){
queue<int> q;q.push(1);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=0;i<26;i++){
int w=t[u].ch[i],v=t[u].fail;
while(!t[v].ch[i])v=t[v].fail;
v=t[v].ch[i];
if(w){
t[w].fail=v;
t[w].id|=t[v].id;//***
q.push(w);
}else t[u].ch[i]=v;//***
}
}
}
void solve(){
queue<Node1> qn;
qn.push((Node1){1,0,0});
dp[1][0][0]=1;
while(!qn.empty()){
Node1 u=qn.front();qn.pop();
vis[u.id][u.tmp][u.now]=0;
if(u.tmp>n)continue;
for(int i=0;i<26;i++){
int id=t[u.id].ch[i],tmp=u.tmp+1,now=u.now|t[id].id;
dp[id][tmp][now]+=dp[u.id][u.tmp][u.now];
if(dp[id][tmp][now]>Mod)dp[id][tmp][now]-=Mod;
if(!vis[id][tmp][now]){
vis[id][tmp][now]=1;
qn.push((Node1){id,tmp,now});
}
}
}
}
int count(int t){
int ans=0;
while(t){
if(t&1)ans++;
t>>=1;
}
return ans;
}
int main(){
for(int i=0;i<1024;i++)c[i]=count(i);
while(1){
scanf("%d%d%d",&n,&m,&p);
if(!n)break;
init();//初始化
for(int i=1;i<=m;i++){
scanf("%s",s[i]);
insert(s[i],i);
}
buildFail();
solve();
int ans=0,up=1<<m;
for(int i=0;i<up;i++)if(c[i]>=p)
for(int j=1;j<=tot;j++){
ans+=dp[j][n][i];
if(ans>Mod)ans-=Mod;
}
printf("%d\n",ans);
}
return 0;
}

HDU2825 Wireless Password 【AC自动机】【状压DP】的更多相关文章

  1. HDU2825 Wireless Password —— AC自动机 + 状压DP

    题目链接:https://vjudge.net/problem/HDU-2825 Wireless Password Time Limit: 2000/1000 MS (Java/Others)    ...

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

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

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

    题目大意:给一系列字符串,用小写字母构造出长度为n的至少包含k个字符串的字符串,求能构造出的个数. 题目分析:在AC自动机上走n步,至少经过k个单词节点,求有多少种走法. 代码如下: # includ ...

  4. 【HDU2825】Wireless Password (AC自动机+状压DP)

    Wireless Password Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u De ...

  5. hdu_2825_Wireless Password(AC自动机+状压DP)

    题目链接:hdu_2825_Wireless Password 题意: 给你m个串,问长度为n至少含k个串的字符串有多少个 题解: 设dp[i][j][k]表示考虑到长度为i,第j个自动机的节点,含有 ...

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

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

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

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

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

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

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

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

  10. 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. UVa 11324 最大团(强连通分量缩点)

    https://vjudge.net/problem/UVA-11324 题意:给一张有向图G,求一个结点数最大的结点集,使得该结点集中任意两个结点u和v满足,要么u可以到达v,要么v可以达到u. 思 ...

  2. codeforces781D Axel and Marston in Bitland

    题目链接:codeforces781D 正解:$bitset$+状压$DP$ 解题报告: 考虑用$f[t][0.1][i][j]$表示从$i$出发走了$2^t$步之后走到了$j$,且第一步是走的$0$ ...

  3. log4j和logback

    Log4j和logback Log4j简介 Log4j(log for java) 1.是Apache的一个开源项目: 2.是使用Java语言编写的一个日志框架: 3.用于记录程序中的日志信息: 4. ...

  4. 图 Graph-图的相关算法

    2018-03-06 17:42:02 一.最短路问题 问题描述:在网络中,求两个不同顶点之间的所有路径中,边的权值之和最小的那一条路径. 这条路径就是两点之间的最短路径 (Shortest Path ...

  5. 场景设计以及Manual Scenario和Goal-OrientedScenario的区别

    Manual Scenario 手工场景 主要是设计用户变化,通过手工场景可以帮助我们分析系统的性能瓶颈.手动方案:如果要生成手动方案,请选择此方法.通过创建组并指定脚本.负载生成器和每组中包括的 V ...

  6. SpringBoot下的值注入

    在我们实际开发项目中,经常会遇到一些常量的配置,比如url,暂时不会改变的字段参数,这个时候我们最好是不要直接写死在代码里的,因为这样编写的程序,应用扩展性太差了,我们可以直接写在配置文件中然后通过配 ...

  7. C#/Java 程序员转GO/golang程序员笔记大全(day 01)

    前言: 整理一下学习 Go 语言的笔记,作为一名老程序,学习一名新的开发语言自然不需要像小白那样从 HelloWorld 看起. 简单整理一下 Go 的一些差异处,希望对大家学习 go 有点帮助,不正 ...

  8. Gruntjs提高生产力(二)

    摆脱混乱的html文件中开发,拥有development与product模式是我们梦寐以求的. 我买的需求是: 1.产出一定格式的目录结构,以供日常开发使用,脚手架功能. 2.在开发模式环境中我们按照 ...

  9. 修改Pycharm for Mac背景色

    Mac 上面的Pycharm的背景是白色,太刺眼,网上教程那么多,实用性都不高,最终在csdn找到了一个. 修改步骤如下: pycharm -->Preferences --> Appea ...

  10. 翻译"Python编程无师自通——专业程序员的养成"

    这本书在 畅销Python编程类入门书,美国亚马逊Kindle编程类排行榜榜一. 开始初学python,也有不少书了,不想在白花钱(买了就放那里不看了),看一个英文文档的原著,准备每天翻译一点,放到b ...