HDU - 2825 Wireless Password(AC自己主动机+DP)
Description
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
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
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)的更多相关文章
- HDU 2825 Wireless Password (AC自己主动机,DP)
pid=2825">http://acm.hdu.edu.cn/showproblem.php? pid=2825 Wireless Password Time Limit: 2000 ...
- hdu 2825 Wireless Password(ac自己主动机&dp)
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Hdu 2457 DNA repair (ac自己主动机+dp)
题目大意: 改动文本串的上的字符,使之不出现上面出现的串.问最少改动多少个. 思路分析: dp[i][j]表示如今 i 个字符改变成了字典树上的 j 节点. 然后顺着自己主动机一直转移方程. 注意合法 ...
- HDU 2825 Wireless Password ( Trie图 && 状态压缩DP )
题意 : 输入n.m.k意思就是给你 m 个模式串,问你构建长度为 n 至少包含 k 个模式串的方案有多少种 分析 : ( 以下题解大多都是在和 POJ 2778 && POJ 162 ...
- HDU 2825 Wireless Password(AC自动机+DP)
题目链接 做题, #include <cstdio> #include <string> #include <cstring> using namespace st ...
- POJ 3691 & 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: ...
- HDU 2896 病毒侵袭 (AC自己主动机)
pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU 2896 病毒侵袭 AC自己主动机题解
本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...
随机推荐
- 杭电OJ_DIY_YTW2_1001 A Mathematical Curiosity
Problem Description Given two integers n and m, count the number of pairs of integers (a,b) such tha ...
- boa-0.94.13:Hello CGI
CGI是什么 CGI全称是CommonGateway Interface,简称CGI,中文名叫做通用网关接口. CGI程序就是符合CGI接口规范的程序,相对于WebServer来说也叫外部程序. CG ...
- 自绘ListBox的两种效果
本文利用Listbox自绘实现了两种特殊效果(见图),左边的风格是自己突然灵感触发想到的,右边的风格来自"C++ Builder 研究"的一个帖子,老妖用BCB实现了,这里则用Delphi实现它. 演 ...
- uva 1335 - Beijing Guards(二分)
题目链接:uva 1335 - Beijing Guards 题目大意:有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个相邻的人拥有同一种礼物, ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)
优先队列(priority queue) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有 ...
- R语言与数据分析之六:时间序列简介
今年在某服装企业蹲点了4个多月,之间非常长一段时间在探索其现货和期货预測.时间序列也是做销售预測的首选,今天和小伙伴分享下时间序列的基本性质和怎样用R来挖据时间序列的相关属性. 首先读入一个时间序列: ...
- WPF入门介绍
Windows Vista已经于2007年1月30正式发行零售版本,安装Vista的计算机将会大量出现.在Vista时代,身为编程员,就一定要具备Vista桌面应用开发的能力.而开发Vista桌面应用 ...
- jsp 中对jar 包的引用
<%@ page language="java" import="你需要的带包名的类" pageEncoding="gb2312"%& ...
- Java魔法堂:JVM的运行模式 (转)
一.前言 JVM有Client和Server两种运行模式.不同的模式对应不同的应用场景,而JVM也会有相应的优化.本文将记录JVM模式的信息,以便日后查阅. 二.介绍 在$JAVA_HOME/jre/ ...
- VC调试篇
难怪很多前辈说调试是一个程序员最基本的技能,其重要性甚至超过学习一门语言.不会调试的程序员就意味着他即使会一门语言,却不能编制出任何好的软件. 我以前接触的程序大多是有比较成形的思路和方法,调试起来出 ...