-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.cpp
More file actions
54 lines (50 loc) · 950 Bytes
/
trie.cpp
File metadata and controls
54 lines (50 loc) · 950 Bytes
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
struct Node {
int c = 0;
int next[26] = {0};
};
vector<Node> tr;
bool ans = 1;
void add(char s[]) {
int cur = 0;
for (int i = 0; s[i]; ++i) {
if (tr[cur].next[s[i] - 'a'] == 0) {
tr[cur].next[s[i] - 'a'] = tr.size();
tr.pb(Node());
}
cur = tr[cur].next[s[i] - 'a'];
}
tr[cur].c++;
}
int query(char s[]) {
int cur = 0;
for (int i = 0; s[i] != '\0'; ++i) {
int nx = tr[cur].next[s[i] - 'a'];
if (nx == 0) return 0;
cur = nx;
}
return tr[cur].c;
}
char s[2000006];
int main() {
int T;
scanf("%d", &T);
while (T--) {
tr.clear();
tr.push_back(Node());
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%s", s);
add(s);
}
}
return 0;
}