hdu2825-AC自动机-状压dp

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2825

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

题意
给出m个魔法串,问有多少个长度为n的串,包含m个串中任意k个串。

思路
因为魔法串数量较少,所以可以把选择的状态状压成2进制表示,例如:0100010000表示为包含第5个和第9个魔法串时的状态。dp[i][j][k]表示长度为i,遍历到AC自动机编号为j的点,包含串的状态为k时的数量,转移为dp[i + 1][AC[j][t]][end[AC[j][t]] | k] += dp[i][j][k];最后统计dp[n][0到AC自动机编号上限][当前二进制下有k到m个1]的和。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int mod = 20090717;
int n, m, kk;
int num[1030];
int dp[30][105][1030];
struct Trie {
int AC[105][26];
int fail[105];
int numend[105];
int len;
int root;
int newcode() {
memset(AC[len], 0, sizeof(AC[len]));
numend[len++] = 0;
return len - 1;
}
void init() {
len = 0;
root = newcode();
}
void build(string s, int st) {
int len = s.length();
int now = root;
for (int i = 0; i < len; i++) {
if (!AC[now][s[i] - 'a']) {
AC[now][s[i] - 'a'] = newcode();
}
now = AC[now][s[i] - 'a'];
}
numend[now] |= (1 << st);
}
void get_fail() {
queue<int>q;
for (int i = 0; i < 26; i++)
if (AC[root][i]) {
fail[AC[root][i]] = root;
q.push(AC[root][i]);
}
else
AC[root][i] = root;
while (!q.empty()) {
int now = q.front();
q.pop();
numend[now] |= numend[fail[now]];
for (int i = 0; i < 26; i++) {
if (!AC[now][i]) {
AC[now][i] = AC[fail[now]][i];
continue;
}
fail[AC[now][i]] = AC[fail[now]][i];
q.push(AC[now][i]);
}
}
}
int slove() {
for (int i = 0; i <= n; i++) {
for (int j = 0; j < len; 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 < len; j++) {
for (int k = 0; k < (1 << m); k++) {
if (dp[i][j][k] > 0) {
for (int t = 0; t < 26; t++) {
dp[i + 1][AC[j][t]][numend[AC[j][t]] | k] += dp[i][j][k];
dp[i + 1][AC[j][t]][numend[AC[j][t]] | k] %= mod;
}
}
}

}
}
int ans = 0;
for (int i = 0; i < len; i++) {
for (int j = 0; j < (1 << m); j++) {
if (num[j] >= kk) {
ans += dp[n][i][j];
ans %= mod;
}
}
}
return ans;
}

}ac;
string s;
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, &kk) != EOF) {
if (n == 0 && m == 0 && kk == 0)
break;
ac.init();
for (int i = 0; i < m; i++) {
cin >> s;
ac.build(s, i);
}
ac.get_fail();
printf("%d\n", ac.slove());
}
}

谢谢你请我吃糖果

你可能感兴趣的:(hdu2825-AC自动机-状压dp)