Skip to content

Commit 2740509

Browse files
lrvideckisweb-flow
andauthored
trie golf (#158)
* trie golf * [auto-verifier] verify commit a2cd225 --------- Co-authored-by: GitHub <[email protected]>
1 parent 5a49519 commit 2740509

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

.verify-helper/timestamps.remote.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
"tests/library_checker_aizu_tests/strings/single_matching_bs.test.cpp": "2025-08-05 19:19:23 -0600",
125125
"tests/library_checker_aizu_tests/strings/suffix_array.test.cpp": "2025-08-06 09:59:41 -0600",
126126
"tests/library_checker_aizu_tests/strings/suffix_array_short.test.cpp": "2025-08-05 19:19:23 -0600",
127-
"tests/library_checker_aizu_tests/strings/trie.test.cpp": "2024-12-05 10:41:42 -0600",
127+
"tests/library_checker_aizu_tests/strings/trie.test.cpp": "2025-08-06 19:25:08 -0600",
128128
"tests/library_checker_aizu_tests/strings/wildcard_pattern_matching.test.cpp": "2025-08-05 19:19:23 -0600",
129129
"tests/library_checker_aizu_tests/trees/count_paths_per_length.test.cpp": "2025-08-06 13:48:07 -0600",
130130
"tests/library_checker_aizu_tests/trees/edge_cd_contour_range_query.test.cpp": "2025-08-06 19:02:15 -0600",

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)