Wireless Password(AC自动机+状压DP)

描述

传送门:HDU2825

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

1
2
3
4
5
6
7
10 2 2
hello
world
4 1 1
icpc
10 0 0
0 0 0

Sample Output

1
2
3
2
1
14195065

思路

  • 题意:长度为$n$的字符串包含至少$k$个串的情况有多少种。
  • 模式串集合很少,可以考虑状态压缩。
  • 在AC自动机上DP即可。状态表示$dp[i][j][mask]$表示长度为$i$的串,在自动机的$j$结点,包含模式串集合为$mask$的组合数。$dp[0][0][0] = 1$。
  • 这样最后的答案就是$\sum dp[n][j][mask]$(其中$mask$的二进制表示中$1$的个数大于等于$k$)
  • 一个数的二进制有多少个$1$可以用gcc的内置函数__builtin_popcount()

代码

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
#include <bits/stdc++.h>
using namespace std;
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define X first
#define Y second
#define fastin \
ios_base::sync_with_stdio(0); \
cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int mod = 20090717;
const double eps = 1e-6;

const int maxn = 105;
struct Trie
{
int ch[maxn][26], f[maxn], val[maxn];
int sz, rt;
int newnode()
{
clr(ch[sz], -1), val[sz] = 0;
return sz++;
}
void init() { sz = 0, rt = newnode(); }
inline int idx(char c) { return c - 'a'; };
void insert(const char* s, const int id)
{
int u = 0, n = strlen(s);
for (int i = 0; i < n; i++)
{
int c = idx(s[i]);
if (ch[u][c] == -1) ch[u][c] = newnode();
u = ch[u][c];
}
val[u] |= (1 << id);
}
void build()
{
queue<int> q;
f[rt] = rt;
for (int c = 0; c < 26; c++)
{
if (~ch[rt][c])
f[ch[rt][c]] = rt, q.push(ch[rt][c]);
else
ch[rt][c] = rt;
}
while (!q.empty())
{
int u = q.front();
q.pop();
val[u] |= val[f[u]];
for (int c = 0; c < 26; c++)
{
if (~ch[u][c])
f[ch[u][c]] = ch[f[u]][c], q.push(ch[u][c]);
else
ch[u][c] = ch[f[u]][c];
}
}
}
int dp[30][maxn][1 << 10];
int solve(int n, int m, int kk)
{
clr(dp, 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])
for (int c = 0; c < 26; c++)
{
int jj = ch[j][c];
(dp[i + 1][jj][k | val[jj]] += dp[i][j][k]) %= mod;
}
int ans = 0;
for (int i = 0; i < (1 << m); i++)
if (__builtin_popcount(i) >= kk)
for (int j = 0; j < sz; j++)
(ans += dp[n][j][i]) %= mod;
return ans;
}
};

Trie ac;

int main()
{
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
int n, m, k;
char buf[30];
while (~scanf("%d%d%d", &n, &m, &k) && 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(n, m, k));
}
return 0;
}
捐助作者
0%