-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0676.cpp
More file actions
56 lines (50 loc) · 1.68 KB
/
Copy path0676.cpp
File metadata and controls
56 lines (50 loc) · 1.68 KB
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
class MagicDictionary {
struct Node {
Node(){};
Node *children[27] = {nullptr};
bool &terminate = reinterpret_cast<bool &>(children[0]);
};
Node *trie = new Node();
public:
void buildDict(const vector<string> &dictionary) {
// create trie and fill it with words
for (const auto &word : dictionary) {
Node *crnt = trie;
for (const char c : word) {
const int idx = c & 0x1F;
if (!crnt->children[idx]) crnt->children[idx] = new Node();
crnt = crnt->children[idx];
}
crnt->terminate = true;
}
}
bool search(const string &searchWord) {
typedef pair<const Node *, int> entry;
stack<entry> st;
const int n = size(searchWord);
// generate all posible char changes that make sense
const Node *crnt = trie;
for (int i = 0; i < n; i++) {
const int idx = searchWord[i] & 0x1F;
for (int j = 1; j <= 26; j++) {
if (j == idx) continue;
if (crnt->children[j]) st.emplace(crnt->children[j], i + 1);
}
if (!crnt->children[idx]) break;
crnt = crnt->children[idx];
}
// check are any of them valid
while (!st.empty()) {
auto [crnt, start] = st.top();
st.pop();
for (int i = start; i < n; i++) {
const int idx = searchWord[i] & 0x1F;
if (!crnt->children[idx]) goto next;
crnt = crnt->children[idx];
}
if (crnt->terminate) return true;
next:;
}
return false;
}
};