UVa 1076 - Password Suspects

contents

  1. 1. Problem
  2. 2. Sample Input
  3. 3. Sample Output
  4. 4. Solution

Problem

給定 N 個單字,請問在長度為 M 的密碼中,密碼符合出現這 N 個單詞的情況有多少種。

在密碼個數少於 42 種時,將所有密碼輸出。

Sample Input

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

Sample Output

1
2
3
4
5
6
Case 1: 2 suspects
helloworld
worldhello
Case 2: 141167095653376 suspects
Case 3: 1 suspects
icpc

Solution

建立 AC 自動機,使用 AC 自動機上的 dp,對於每一個 node 將單字用 2^N 來表示 dp 狀態,因此每一個 node 的狀態為 dp[len][1<<N] 表示匹配長度 len,並且已經 match 到的狀態。

由於要輸出 42 以內的密碼可能,在輸出答案前,進行回溯標記,確保下一步可以抵達到可行解,接著進行 dfs 搜索。

1
2
3
4
5
6
7
8
9
1 2
a
a
1 0
2 2
b
ab

特別小心測資存在兩個單詞相同、一個單詞是另一個單詞的 substring。

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <map>
#include <algorithm>
#include <assert.h>
#define MAXCHAR 26
#define MAXS (1024)
#define MAXNODE 256
#pragma comment( linker, "/STACK:1024000000,1024000000")
using namespace std;
class ACmachine {
public:
struct Node {
Node *next[MAXCHAR], *fail;
int cnt, val, id;
long long dp[30][1024];
int dpable[30][1024];
void init() {
fail = NULL;
cnt = val = 0;
id = 0;
memset(next, 0, sizeof(next));
memset(dp, 0, sizeof(dp));
memset(dpable, 0, sizeof(dp));
}
} nodes[MAXNODE];
Node *root;
int size;
Node* getNode() {
Node *p = &nodes[size++];
p->init();
return p;
}
void init() {
size = 0;
root = getNode();
}
int toIndex(char c) {
return c - 'a';
}
void dfs(Node *p, Node *q) {
for (int i = 0; i < MAXCHAR; i++) {
if (q->next[i]) {
Node *u = p->next[i], *v = q->next[i];
if (u == NULL)
p->next[i] = getNode(), u = p->next[i];
u->cnt |= v->cnt;
dfs(u, v);
}
}
}
void merge(const ACmachine &other) {
dfs(root, other.root);
}
void insert(const char str[], int sid) {
Node *p = root;
for (int i = 0, idx; str[i]; i++) {
idx = toIndex(str[i]);
if (p->next[idx] == NULL)
p->next[idx] = getNode();
p = p->next[idx];
}
p->cnt = 1;
if (sid >= 0) p->id |= 1<<sid;
}
int find(const char str[]) {
Node *p = root;
for (int i = 0, idx; str[i]; i++) {
idx = toIndex(str[i]);
if (p->next[idx] == NULL)
p->next[idx] = getNode();
p = p->next[idx];
}
return p->cnt;
}
void build() { // AC automation
queue<Node*> Q;
Node *u, *p;
Q.push(root), root->fail = NULL;
while (!Q.empty()) {
u = Q.front(), Q.pop();
for (int i = 0; i < MAXCHAR; i++) {
if (u->next[i] == NULL)
continue;
Q.push(u->next[i]);
p = u->fail;
while (p != NULL && p->next[i] == NULL)
p = p->fail;
if (p == NULL)
u->next[i]->fail = root;
else
u->next[i]->fail = p->next[i];
u->next[i]->val = u->next[i]->fail->val + u->next[i]->cnt;
u->next[i]->id = u->next[i]->fail->id | u->next[i]->id;
}
}
}
int query(const char str[]) {
Node *u = root, *p;
int matched = 0;
for (int i = 0, idx; str[i]; i++) {
idx = toIndex(str[i]);
while (u->next[idx] == NULL && u != root)
u = u->fail;
u = u->next[idx];
u = (u == NULL) ? root : u;
p = u;
matched += p->val;
}
return matched;
}
long long dp(int len, int N) {
queue<Node*> Q;
Node *u, *p;
root->dp[0][0] = 1;
long long ret = 0;
for (int k = 0; k <= len; k++) {
Q.push(root), ret = 0;
while (!Q.empty()) {
u = Q.front(), Q.pop();
ret += u->dp[len][(1<<N)-1];
if (u->dp[len][(1<<N)-1])
u->dpable[len][(1<<N)-1] = 1;
for (int i = 0; i < (1<<N); i++) {
if (i && u->dp[k][i] == 0)
continue;
for (int j = 0; j < MAXCHAR; j++) {
if (u->next[j] != NULL)
if (i == 0) Q.push(u->next[j]);
if (u->dp[k][i] == 0)
continue;
p = u;
while (p != root && p->next[j] == NULL)
p = p->fail;
p = p->next[j];
if (p == NULL) continue;
if (p->id)
p->dp[k+1][i|p->id] += u->dp[k][i];
else
p->dp[k+1][i] += u->dp[k][i];
}
}
} // <end queue>
}
// backtrack
for (int k = len-1; k >= 0; k--) {
Q.push(root);
while (!Q.empty()) {
u = Q.front(), Q.pop();
for (int i = 0; i < (1<<N); i++) {
if (i && u->dp[k][i] == 0)
continue;
for (int j = 0; j < MAXCHAR; j++) {
if (u->next[j] != NULL)
if (i == 0) Q.push(u->next[j]);
if (u->dp[k][i] == 0)
continue;
p = u;
while (p != root && p->next[j] == NULL)
p = p->fail;
p = p->next[j];
if (p == NULL) continue;
if (p->id >= 0) {
if (p->dpable[k+1][i|p->id])
u->dpable[k][i] = 1;
} else {
if (p->dpable[k+1][i])
u->dpable[k][i] = 1;
}
}
}
} // <end queue>
}
return ret;
}
void dpSearch(Node *u, int s, int len, int N, string str, vector<string> &ret) {
if (str.length() == len) {
if (s == (1<<N)-1)
ret.push_back(str);
return ;
}
if (u->dpable[str.length()][s] == 0)
return ;
Node *p;
for (int i = 0; i < MAXCHAR; i++) {
p = u;
while (p != root && p->next[i] == NULL)
p = p->fail;
p = p->next[i];
if (p == NULL) continue;
int ns = s;
if (p->id >= 0)
ns = s|p->id;
dpSearch(p, ns, len, N, str + (char) (i + 'a'), ret);
}
}
void free() {
return ;
// owner memory pool version
queue<Node*> Q;
Q.push(root);
Node *u;
while (!Q.empty()) {
u = Q.front(), Q.pop();
for (int i = 0; i < MAXCHAR; i++) {
if (u->next[i] != NULL) {
Q.push(u->next[i]);
}
}
delete u;
}
}
};
ACmachine g;
int main() {
int M, N, cases = 0;
char s[1024];
while (scanf("%d %d", &M, &N) == 2 && M+N) {
g.init();
for (int i = 0; i < N; i++) {
scanf("%s", s);
g.insert(s, i);
}
for (int i = 'a'; i <= 'z'; i++) {
s[0] = i, s[1] = '\0';
g.insert(s, -1);
}
g.build();
long long way = g.dp(M, N);
printf("Case %d: %lld suspects\n", ++cases, way);
if (way <= 42) {
vector<string> pwd;
g.dpSearch(g.root, 0, M, N, "", pwd);
sort(pwd.begin(), pwd.end());
for (int i = 0; i < pwd.size(); i++)
printf("%s\n", pwd[i].c_str());
}
g.free();
}
return 0;
}
/*
10 2
hello
world
10 0
4 1
icpc
10 3
mo
mom
omsi
4 4
a
b
c
d
4 3
ab
bc
ca
1 2
a
a
1 0
2 2
b
ab
0 0
*/