题目链接:https://vjudge.net/problem/HDU-2825

Wireless Password

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7733    Accepted Submission(s): 2509

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
 
Source

题意:

给出m个单词,问长度为n且至少含有k个单词的字符串有多少个?

题解:

1.把这m个单词插入到AC自动机中。

2.设dp[i][j][status]为:长度为i,到达j状态(AC自动机中的状态),且含有单词的信息为status(状态压缩)的字符串有多少个。

3.模拟在AC自动机上的跳动,求出dp数组。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = ;
const int MAXN = +; int num[<<], dp[][][<<]; struct Trie
{
const static int sz = , base = 'a';
int next[MAXN][sz], fail[MAXN], end[MAXN];
int root, L;
int newnode()
{
for(int i = ; i<sz; i++)
next[L][i] = -;
end[L++] = ;
return L-;
}
void init()
{
L = ;
root = newnode();
}
void insert(char buf[], int id)
{
int len = strlen(buf);
int now = root;
for(int i = ; i<len; i++)
{
if(next[now][buf[i]-base] == -) next[now][buf[i]-base] = newnode();
now = next[now][buf[i]-base];
}
end[now] |= (<<id);
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = ; i<sz; i++)
{
if(next[root][i] == -) next[root][i] = root;
else fail[next[root][i]] = root, Q.push(next[root][i]);
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
end[now] |= end[fail[now]]; //当前串的后缀是否也包含单词
for(int i = ; i<sz; i++)
{
if(next[now][i] == -) next[now][i] = next[fail[now]][i];
else fail[next[now][i]] = next[fail[now]][i], Q.push(next[now][i]);
}
}
} int query(int n, int m, int k)
{
for(int i = ; i<=n; i++)
for(int j = ; j<L; j++)
for(int s = ; s<(<<m); s++)
dp[i][j][s] = ;
dp[][][] = ;
for(int i = ; i<n; i++) //模拟在AC自动机上的跳动
for(int j = ; j<L; j++)
for(int s = ; s<(<<m); s++)
{
if(dp[i][j][s]==) continue;
for(int p = ; p<sz; p++)
{
int newi = i+;
int newj = next[j][p];
int news = (s|end[newj]);
dp[newi][newj][news] += dp[i][j][s];
dp[newi][newj][news] %= MOD;
}
}
int ret = ;
for(int s = ; s<(<<m); s++)
{
if(num[s]<k) continue;
for(int i = ; i<L; i++)
ret = (ret+dp[n][i][s])%MOD;
}
return ret;
}
}; Trie ac;
char buf[];
int main()
{
for(int s = ; s<(<<); s++)
{
num[s] = ;
for(int i = ; i<; i++)
if(s&(<<i)) num[s]++;
} int n, m, k;
while(scanf("%d%d%d", &n,&m,&k)&&(n||m||k))
{
ac.init();
for(int i = ; i<m; i++)
{
scanf("%s", buf);
ac.insert(buf, i);
}
ac.build();
int ans = ac.query(n,m,k);
printf("%d\n", ans);
}
return ;
}

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

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

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

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

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

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

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

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

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

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

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

  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. 【音乐App】—— Vue-music 项目学习笔记:推荐页面开发

    前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 上一篇总结了项目概述.项目准备.页面骨架搭建.这一篇重点梳理推荐页面开发.项目github地址:https://github.com/66We ...

  2. C#数据之DataTable

    C#创建DataTable的几种方式 第一种方式:直接添加数据对象 DataTable table = new DataTable(); table.Columns.Add("strName ...

  3. C 作用域规则

    C 作用域规则 任何一种编程中,作用域是程序中定义的变量所存在的区域,超过该区域变量就不能被访问.C 语言中有三个地方可以声明变量: 在函数或块内部的局部变量 在所有函数外部的全局变量 在形式参数的函 ...

  4. npm安装package.json

    npm安装package.json时,直接转到当前项目目录下,用命令npm install 或npm install --save-dev安装即可,自动将package.json中的模块安装到node ...

  5. 笔记本WIFI卡简介

    1.Intel AC9560(CNVI) AC9260(pcie) 3165D2W(pcie) 2.Realtek瑞昱 RTL8822be(pcie) RTL8723BU(USB) 英特尔在300系主 ...

  6. 字符串各个字符ASCII值加5

    程序实现目标: 输入一个字符串,将其各个字符对应的ASCII值加5后,输出结果 程序要求:该字符串只包含小写字母,若其值加5后的字符值大于'z',将其转换成从a开始的字符. 分析:问题归结为三点: 1 ...

  7. NativeBase准备工作

    环境 node>= 4.0 npm>= 3.0 rnpm (only if React Native version < 0.29) ReactNativeCLI  安装及运行 ht ...

  8. IPv4地址(二)网络划分

    在IPv4地址(一)中提到过,IP地址可以分成两部分,前面一部分是网络号,而后面一部分是主机号. 这里网络可以通过主机数量规模不同而分为3类:大型网络.中型网络和小型网络. 不同网络的特点 大型网络— ...

  9. hibernate的一级缓存和二级缓存详解

    hibernate为我们提供了一级缓存和二级缓存,目的是为了减少应用程序对数据库的访问次数. 一级缓存: (1)所谓一级缓存就是session级别的缓存,当我们使用他的范围是当前的session,当s ...

  10. 【SQLServer2008】之改变主键当为null时也不会报错,可以入数据库。

    在SqlServer红框中设置主键,右键会有添加主键选项,并且设置不能为null. 当我们插入主键数据如果为null时,会插不进去,这时候我们需要修改一下,如下图: “标识规范”中选择“是”,就可以了 ...