forked from gitter-badger/Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp1014.cpp
More file actions
61 lines (54 loc) · 1.14 KB
/
p1014.cpp
File metadata and controls
61 lines (54 loc) · 1.14 KB
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
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
struct TrieNode {
int son[26];
int cnt;
void init() {
cnt = 0;
memset(son, -1, sizeof(son));
}
};
int tot;
TrieNode trie[1000000];
void insert(char *buf) {
int cur = 0;
for (int i = 0; buf[i]; i++) {
if (trie[cur].son[buf[i]-'a'] == -1) {
trie[tot].init();
trie[cur].son[buf[i]-'a'] = tot++;
}
cur = trie[cur].son[buf[i]-'a'];
trie[cur].cnt++;
}
}
int query(char *buf) {
int cur = 0;
for (int i = 0; buf[i]; i++) {
if (trie[cur].son[buf[i]-'a'] == -1) {
return 0;
}
cur = trie[cur].son[buf[i]-'a'];
}
return trie[cur].cnt;
}
char buf[22];
int main() {
int n, m;
while (~scanf("%d", &n)) {
trie[0].init();
tot = 1;
for (int i = 0; i < n; i++) {
scanf("%s", buf);
insert(buf);
}
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%s", buf);
printf("%d\n", query(buf));
}
}
return 0;
}