-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5b7502
commit a32e95c
Showing
1 changed file
with
14 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,36 @@ | ||
#include <string> | ||
#include <vector> | ||
|
||
// CUT begin | ||
struct Trie { | ||
char a_init; | ||
int D; | ||
int INVALID = -1; | ||
std::vector<std::vector<int>> child; | ||
std::vector<int> par; | ||
|
||
using T_NODE = int; | ||
std::vector<T_NODE> v_info; | ||
|
||
Trie(char a_init = 'a', int D = 26) | ||
: a_init(a_init), D(D), child(1, std::vector<int>(D, INVALID)), v_info(1) {} | ||
void add_word(const std::string &str, T_NODE info) { | ||
: a_init(a_init), D(D), child(1, std::vector<int>(D, INVALID)), par(1, -1), v_info(1) {} | ||
|
||
int step(int now, char c) const { | ||
if (now == INVALID) return INVALID; | ||
return child.at(now).at(c - a_init); | ||
} | ||
|
||
int add_word(const std::string &str, T_NODE info) { | ||
int now = 0; | ||
for (auto &c : str) { | ||
if (child[now][c - a_init] == INVALID) { | ||
par.push_back(now); | ||
child[now][c - a_init] = child.size(); | ||
child.emplace_back(std::vector<int>(D, INVALID)); | ||
child.emplace_back(D, INVALID); | ||
v_info.resize(child.size()); | ||
} | ||
now = child[now][c - a_init]; | ||
} | ||
v_info[now] += info; | ||
return now; | ||
} | ||
}; |