-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtrie_static.cpp
110 lines (91 loc) · 2.26 KB
/
trie_static.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <bits/stdc++.h>
using namespace std;
// The total length of all the string, and the size of the alphabet.
const int N = 100100, ALPA_SIZE = 255;
int trie[N][ALPA_SIZE]; // The trie.
int nodesCount; // The number of nodes in the trie.
int distinctWordsCount; // The number of distinct word in the trie.
int wordsCount[N]; // Number of words sharing node "i"
int wordsEndCount[N]; // Number of words ending at node "i"
/**
* Initializes the trie.
* Must be called before each test case.
*/
void init() {
nodesCount = 0;
distinctWordsCount = 0;
memset(trie, -1, sizeof(trie));
memset(wordsCount, 0, sizeof(wordsCount));
memset(wordsEndCount, 0, sizeof(wordsEndCount));
}
/**
* Outs a new edge from the given node if not exists.
*
* @param id the node id to out the edge from
* @param c the type (or the character) of the edges.
*
* @return the id of the node following the added edge.
*/
int addEdge(int id, char c) {
int& nxt = trie[id][c];
if (nxt == -1) {
nxt = ++nodesCount;
}
return nxt;
}
/**
* Inserts a new word in the trie.
*
* Complexity: O(n)
*
* @param str the word to insert.
*/
void insert(const char* str) {
int cur = 0;
for (int i = 0; str[i]; ++i) {
wordsCount[cur]++;
cur = addEdge(cur, str[i]);
}
wordsCount[cur]++;
distinctWordsCount += (++wordsEndCount[cur] == 1);
}
/**
* Removes a word from the trie assuming that it was added before.
*
* Complexity: O(n)
*
* @param str the word to erase.
*/
void erase(const char* str) {
int cur = 0;
for (int i = 0; str[i]; ++i) {
wordsCount[cur]--;
int nxt = trie[cur][str[i]];
if (wordsCount[nxt] == 1) {
trie[cur][str[i]] = -1;
}
cur = nxt;
}
wordsCount[cur]--;
distinctWordsCount -= (--wordsEndCount[cur] == 0);
}
/**
* Searches for a word in the trie.
*
* Complexity: O(n)
*
* @param str the word to search for.
*
* @return the number of occurrences of the given word.
*/
int search(const char* str) {
int cur = 0;
for (int i = 0; str[i]; ++i) {
int nxt = trie[cur][str[i]];
if (nxt == -1) {
return 0;
}
cur = nxt;
}
return wordsEndCount[cur];
}