Skip to content

Commit a32e95c

Browse files
committed
update light trie
1 parent e5b7502 commit a32e95c

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

string/trie_light.hpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
#include <string>
22
#include <vector>
33

4-
// CUT begin
54
struct Trie {
65
char a_init;
76
int D;
87
int INVALID = -1;
98
std::vector<std::vector<int>> child;
9+
std::vector<int> par;
10+
1011
using T_NODE = int;
1112
std::vector<T_NODE> v_info;
13+
1214
Trie(char a_init = 'a', int D = 26)
13-
: a_init(a_init), D(D), child(1, std::vector<int>(D, INVALID)), v_info(1) {}
14-
void add_word(const std::string &str, T_NODE info) {
15+
: a_init(a_init), D(D), child(1, std::vector<int>(D, INVALID)), par(1, -1), v_info(1) {}
16+
17+
int step(int now, char c) const {
18+
if (now == INVALID) return INVALID;
19+
return child.at(now).at(c - a_init);
20+
}
21+
22+
int add_word(const std::string &str, T_NODE info) {
1523
int now = 0;
1624
for (auto &c : str) {
1725
if (child[now][c - a_init] == INVALID) {
26+
par.push_back(now);
1827
child[now][c - a_init] = child.size();
19-
child.emplace_back(std::vector<int>(D, INVALID));
28+
child.emplace_back(D, INVALID);
2029
v_info.resize(child.size());
2130
}
2231
now = child[now][c - a_init];
2332
}
2433
v_info[now] += info;
34+
return now;
2535
}
2636
};

0 commit comments

Comments
 (0)