-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrieArray.cpp
More file actions
35 lines (30 loc) · 820 Bytes
/
trieArray.cpp
File metadata and controls
35 lines (30 loc) · 820 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
// __init the data-structure in every case
// keep next [] as less sized as possible, modify hash ()
int tot;
struct node {
int endPos;
int next [];
node () {
endPos = 0;
memset (next, -1, sizeof next);
}
} state [MAX_N];
inline int hash (char ch) { return -1; }
void __init () { tot = 0; state [tot++] = node (); }
void insert (char *str){
int cur = 0;
for (int i = 0; str [i]; ++i) {
if (state [cur].next [hash (str [i])] == -1) {
state [tot] = node ();
state [cur].next [hash (str [i])] = tot++;
}
cur = state [cur].next [hash (str [i])];
}
++state [cur].endPos;
}
void dfs (int cur) {
for (int i = 0; i < 128; ++i) {
if (state [cur].next [i] == -1) continue;
dfs (state [cur].next [i]);
}
}