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

题意:让你构造一个长度为n的字符串。使得至少包括共m个的集合里的k个,求个数。

思路:AC自己主动机构造。须要用到DP的思想,设dp[i][j][k]表示长度为i到达自己主动机状态为j的时候且状态k时的个数。

我们须要用一个来表示状态j包括的字符串的个数。也就是走到字符串末尾的个数,二进制表示。

然后还要注意一点的是:我们在构造自己主动机的时候有fail指针的转移。所以状态j也须要结合它失配时候的状态。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int mod = 20090717; int n, m, k;
int dp[30][250][1<<10];
int num[5000]; struct Trie {
int nxt[110][26], fail[110], end[110];
int root, sz; int newNode() {
for (int i = 0; i < 26; i++)
nxt[sz][i] = -1;
end[sz++] = 0;
return sz - 1;
} void init() {
sz = 0;
root = newNode();
} void insert(char buf[], int id) {
int now = root;
for (int i = 0; buf[i]; i++) {
if (nxt[now][buf[i]-'a'] == -1)
nxt[now][buf[i]-'a'] = newNode();
now = nxt[now][buf[i]-'a'];
}
end[now] |= (1<<id);
} void build() {
queue<int> q;
fail[root] = root;
for (int i = 0; i < 26; i++) {
if (nxt[root][i] == -1)
nxt[root][i] = root;
else {
fail[nxt[root][i]] = root;
q.push(nxt[root][i]);
}
} while (!q.empty()) {
int now = q.front();
q.pop();
end[now] |= end[fail[now]];
for (int i = 0; i < 26; i++) {
if (nxt[now][i] == -1)
nxt[now][i] = nxt[fail[now]][i];
else {
fail[nxt[now][i]] = nxt[fail[now]][i];
q.push(nxt[now][i]);
}
}
}
} int solve() {
for (int i = 0; i <= n; i++)
for (int j = 0; j < sz; j++)
for (int k = 0; k < (1<<m); k++)
dp[i][j][k] = 0;
dp[0][0][0] = 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < sz; j++)
for (int k = 0; k < (1<<m); k++)
if (dp[i][j][k] > 0) {
for (int x = 0; x < 26; x++) {
int ni = i+1;
int nj = nxt[j][x];
int nk = k | end[nj];
dp[ni][nj][nk] += dp[i][j][k];
dp[ni][nj][nk] %= mod;
}
}
int ans = 0;
for (int p = 0; p < (1<<m); p++) {
if (num[p] < k) continue;
for (int i = 0; i < sz; i++)
ans = (ans + dp[n][i][p]) % mod;
}
return ans;
}
} ac;
char buf[20]; int main() {
for (int i = 0; i < (1<<10); i++) {
num[i] = 0;
for (int j = 0; j < 10; j++)
if (i & (1<<j))
num[i]++;
} while (scanf("%d%d%d", &n, &m, &k) != EOF && n+m+k) {
ac.init();
for (int i = 0; i < m; i++) {
scanf("%s", buf);
ac.insert(buf, i);
} ac.build();
printf("%d\n", ac.solve());
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

HDU - 2825 Wireless Password(AC自己主动机+DP)的更多相关文章

  1. HDU 2825 Wireless Password (AC自己主动机,DP)

    pid=2825">http://acm.hdu.edu.cn/showproblem.php? pid=2825 Wireless Password Time Limit: 2000 ...

  2. hdu 2825 Wireless Password(ac自己主动机&amp;dp)

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

  3. Hdu 2457 DNA repair (ac自己主动机+dp)

    题目大意: 改动文本串的上的字符,使之不出现上面出现的串.问最少改动多少个. 思路分析: dp[i][j]表示如今 i 个字符改变成了字典树上的 j 节点. 然后顺着自己主动机一直转移方程. 注意合法 ...

  4. HDU 2825 Wireless Password ( Trie图 && 状态压缩DP )

    题意 : 输入n.m.k意思就是给你 m 个模式串,问你构建长度为 n 至少包含 k 个模式串的方案有多少种 分析 : ( 以下题解大多都是在和 POJ 2778 && POJ 162 ...

  5. HDU 2825 Wireless Password(AC自动机+DP)

    题目链接 做题, #include <cstdio> #include <string> #include <cstring> using namespace st ...

  6. POJ 3691 &amp; HDU 2457 DNA repair (AC自己主动机,DP)

    http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...

  7. HDU 2896 病毒侵袭 (AC自己主动机)

    pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...

  8. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  9. HDU 2896 病毒侵袭 AC自己主动机题解

    本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...

随机推荐

  1. zoj1028-Flip and Shift

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=28 题意:有相互交叉的黑白两种颜色的小球,每一个小球每次可以跳两格:问你是否可以 ...

  2. Java中的throw和throws的差别

    Java中的throw和throws的差别 1.throwkeyword用于方法体内部.而throwskeyword用于方法体部的方法声明部分: 2.throw用来抛出一个Throwable类型的异常 ...

  3. android面试题目大全<完结部分>,android笔试题目集锦

    1. 下列哪些语句关于内存回收的说明是正确的? (b ) A. 程序员必须创建一个线程来释放内存   B.内存回收程序负责释放无用内存    C.内存回收程序允许程序员直接释放内存    D.内存回收 ...

  4. mfc控件与其对应的对象的关联方法

    对话框的控件与其对应类的对象相关联:(两种方法) (1)      通过CWnd::DoDataExchange函数进行关联: 用VC++6.0的MFC ClassWizard中的Member Var ...

  5. Linux经常使用命令(十一) - more

    more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最主要的指令就是按空白键(space)就往下一页显示,按 b 键就会 ...

  6. 【c语言】统计一个数二进制中的1的个数

    // 统计一个数二进制中的1的个数 #include <stdio.h> int count(int a) { int count = 0; while (a) { count++; a ...

  7. 积累的VC编程小技巧之工具条和状态条

    1.工具条和状态条中控件的添加: 方法⑴.只能在ToolBar里创建控件:首先,在ToolBar中创建一个Button,其ID为ID_TOOL_COMBO(我们要将创建的控件放在该Button的位置上 ...

  8. win32 sdk树形控件的项拖拽实现

    本课中,我们将学习如何使用树型视图控件.另外还要学习如何在树型视图中完成拖-拉动作,以及如何使用图象列表. 理论: 树型视图是一种特别的窗口,我们可以使用它一目了然地表示某种层次关系.譬如象在资源管理 ...

  9. Cocos2d-x3.3RC0的Android编译Activity启动流程分析

    本文将从引擎源代码Jni分析Cocos2d-x3.3RC0的Android Activity的启动流程,以下是具体分析. 1.引擎源代码Jni.部分Java层和C++层代码分析 watermark/2 ...

  10. SQL ---指令实例语句

    1 1 create database+数据库名字 创建数据库 2 2 create table+表的名字 创建表 3 表中的操作: 4 3 insert into 表名 (列名1,列名2··)val ...