Skip to content

Commit 609d5a3

Browse files
committed
fix trie
1 parent 65d9577 commit 609d5a3

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

content/string/trie.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ vector<node> trie = {node()};
1010
int traverse(const vector<int>& word, int x) {
1111
int id = 0;
1212
for (int c : word) {
13-
if (id < 0 || (trie[id].words == 0 && x <= 0)) return -1;
13+
if (trie[id].words == 0 && x <= 0) return -1;
1414
trie[id].words += x;
1515
if (trie[id].nxt[c] < 0 && x > 0) {
1616
trie[id].nxt[c] = sz(trie);
1717
trie.emplace_back();
1818
}
1919
id = trie[id].nxt[c];
20+
if (id < 0) return -1;
2021
}
2122
trie[id].words += x;
2223
trie[id].ends += x;
@@ -26,7 +27,6 @@ int traverse(const vector<int>& word, int x) {
2627
int insert(const vector<int>& word) {
2728
return traverse(word, 1);
2829
}
29-
3030
bool erase(const vector<int>& word) {
3131
int id = traverse(word, 0);
3232
if (id < 0 || trie[id].ends <= 0) return false;

0 commit comments

Comments
 (0)