Skip to content

Commit a2cd225

Browse files
committed
trie golf
1 parent 5a49519 commit a2cd225

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

library/strings/trie.hpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
#pragma once
22
//! https://cp-algorithms.com/string/aho_corasick.html#construction-of-the-trie
3-
const int mn = 'A',
4-
cnt_let = 26; // mn <= s[i] < mn + cnt_let
3+
const int mn = 'A';
54
struct trie {
65
struct node {
7-
array<int, cnt_let> next;
8-
int cnt_words = 0, par = -1;
9-
char ch;
10-
node(int par = -1, char ch = '#'): par(par), ch(ch) {
11-
fill(all(next), -1);
12-
}
6+
array<int, 26> next;
7+
bool end_of_word = 0;
8+
node() { fill(all(next), -1); }
139
};
14-
vector<node> t;
10+
deque<node> t;
1511
trie(): t(1) {}
1612
void insert(const string& s) {
1713
int v = 0;
1814
for (char ch : s) {
1915
int u = ch - mn;
2016
if (t[v].next[u] == -1) {
2117
t[v].next[u] = sz(t);
22-
t.emplace_back(v, ch);
18+
t.emplace_back();
2319
}
2420
v = t[v].next[u];
2521
}
26-
t[v].cnt_words++;
22+
t[v].end_of_word = 1;
2723
}
2824
int find(const string& s) {
2925
int v = 0;
@@ -32,6 +28,6 @@ struct trie {
3228
if (t[v].next[u] == -1) return 0;
3329
v = t[v].next[u];
3430
}
35-
return t[v].cnt_words;
31+
return t[v].end_of_word;
3632
}
3733
};

0 commit comments

Comments
 (0)