题目链接: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. AutoCAD如何快速标注零件序号

    1 先画好一条直线和一个数字   2 选中刚才绘制的数字和直线,选择阵列(估计大概要画四十个就阵列四十行,改一下行偏移)   预览效果如图所示   随后不断重复直线即可   横向也是一样   最后双击 ...

  2. MySQL binlog-do-db选项是危险的[转]

    很多人通过 binlog-do-db, binlog-ignore-db, replicate-do-db 和   replicate-ignore-db 来过滤复制(某些数据库), 尽管有些使用, ...

  3. OpenCV生成点集的Delaunay剖分和Voronoi图

    实现内容: 设置一副图像大小为600*600.图像像素值全为0,为黑色. 在图像中Rect(100,100,400,400)的区域随机产生20个点.并画出. 产生这些点集的Delaunay剖分和Vor ...

  4. js高度line-height及宽度text-align:center居中插件

    1.高度居中---在高度设为100%,无法直接使用line-height:100%;会不起效果 这是用于应对height:100%的插件 /** * 高度居中函数,用于应对高度设为100%时的居中 * ...

  5. Rate Monotonic Scheduling algorithm

    这篇文章写得不错 http://barrgroup.com/embedded-systems/How-To/RMA-Rate-Monotonic-Algorithm 另外rtems的官方文档也有类似说 ...

  6. 【GoldenGate】使用OGG,两个Oracle库之间单向同步数据

    ************************************************************************ ****原文:blog.csdn.net/clark_ ...

  7. ssh无密码登陆屌丝指南

    [0]写在前面 由于ssh 实现的是免密码登陆,大致步骤是: 0.1) client通过ssh登陆到server: 0.2) server检查家目录下的.ssh文件, 并发送公钥文件 authoriz ...

  8. zoj 2068 - Chopsticks

    题目:非常多人在一起吃饭.有两组单支的筷子,定义badness为一对筷子长度差的平方,求最小的badness和. 分析:dp,最大公共子序列类似物. 这里利用数学关系找到一个结论: a < b ...

  9. CountDownTimer

    package com.daoge.widget; import java.text.DecimalFormat; import android.os.CountDownTimer; import a ...

  10. opensearch空查询

    query子句不支持为空的查询,可以使用filter子句:filter=area=""   或者 filter=filedlen(area)=0 可以使用相关性函数实现:https ...