Blackops

初心易得,始终难守

0%

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

Wireless Password

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7127 Accepted Submission(s): 2339

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

题目链接:HDU 2825
题意就是求构造长度为$n$,至少包含$k$个给定字符串的字符串方案数。
考虑AC自动机上的dp,$dp[i][j][s]$表示当前构造的字符串长度为$i$,走到了AC自动机第$j$个节点,包含的字符串二进制状态为$s$,然后设$j$的下一步走到$v$那么可以发现:

最后从$dp[len]$中选出二进制状态的$1$大于等于$k$的方案数求和即可,另外这题由于无用的状态太多因此最好用当前状态更新下一个状态,而不是用上一个状态更新当前状态,否则很容易TLE
代码:

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 110;
const int mod = 20090717;
struct Trie
{
int nxt[26];
int fail, cnt;
void init()
{
for (int i = 0; i < 26; ++i)
nxt[i] = -1;
fail = cnt = 0;
}
} L[N];
int sz;
char s[N];
int dp[30][N][1 << 10], one[1 << 10];

namespace AC
{
void init()
{
sz = 0;
L[sz++].init();
}
void ins(char s[], int len, int id)
{
int u = 0;
for (int i = 0; i < len; ++i)
{
int v = s[i] - 'a';
if (L[u].nxt[v] == -1)
{
L[sz].init();
L[u].nxt[v] = sz++;
}
u = L[u].nxt[v];
}
L[u].cnt |= (1 << id);
}
void build()
{
queue<int>Q;
L[0].fail = 0;
for (int i = 0; i < 26; ++i)
{
if (L[0].nxt[i] == -1)
L[0].nxt[i] = 0;
else
{
L[L[0].nxt[i]].fail = 0;
Q.push(L[0].nxt[i]);
}
}
while (!Q.empty())
{
int u = Q.front();
Q.pop();
int uf = L[u].fail;
if (L[uf].cnt)
L[u].cnt |= L[uf].cnt;
for (int i = 0; i < 26; ++i)
{
if (L[u].nxt[i] == -1)
L[u].nxt[i] = L[uf].nxt[i];
else
{
L[L[u].nxt[i]].fail = L[uf].nxt[i];
Q.push(L[u].nxt[i]);
}
}
}
}
}
int main(void)
{
int n, m, k, i, j;
for (i = 0; i < 1024; ++i)
one[i] = bitset<11>(i).count();
while (~scanf("%d%d%d", &n, &m, &k) && (n || m || k))
{
AC::init();
CLR(dp, 0);
for (i = 0; i < m; ++i)
{
scanf("%s", s);
AC::ins(s, strlen(s), i);
}
AC::build();
int R = (1 << m);
dp[0][0][0] = 1;
for (i = 0; i < n; ++i)
{
for (j = 0; j < sz; ++j)
{
for (int st = 0; st < R; ++st)
{
if (dp[i][j][st])
{
for (int l = 0; l < 26; ++l)
{
int v = L[j].nxt[l];
dp[i + 1][v][st | L[v].cnt] += dp[i][j][st];
if (dp[i + 1][v][st | L[v].cnt] >= mod)
dp[i + 1][v][st | L[v].cnt] %= mod;
}
}
}
}
}
int ans = 0;
for (i = 0; i < sz; ++i)
{
for (int s = 0; s < R; ++s)
{
if (one[s] >= k)
{
ans = ans + dp[n][i][s];
if (ans > mod)
ans %= mod;
}
}
}
printf("%d\n", ans);
}
return 0;
}