-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
73 lines (61 loc) · 1.78 KB
/
main.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
class WordFilter {
const char dilimiter = ':';
struct Node {
array<Node*, 27> children = array<Node*, 27>();
int index = -1;
};
Node* root = new Node;
int char2idx(char c) { return c == dilimiter ? 26 : c - 'a'; }
void insert(const string_view& s, int index) {
auto p = root;
for (char c : s) {
int i = char2idx(c);
if (p->children[i] == nullptr) p->children[i] = new Node;
p = p->children[i];
}
p->index = index;
};
Node* search(const string_view& s) {
auto p = root;
for (char c : s) {
int i = char2idx(c);
if (p->children[i] == nullptr) return nullptr;
p = p->children[i];
}
return p;
}
public:
WordFilter(vector<string>& words) {
for (int i = 0; i < words.size(); ++i) {
const string& w = words[i];
string s = w + dilimiter + w;
string_view sv(s);
for (int j = 0; j < w.size(); ++j) {
insert(sv, i);
sv.remove_prefix(1);
}
}
}
int f(string prefix, string suffix) {
string s = suffix + dilimiter + prefix;
string_view sv(s);
Node* p = search(sv);
if (p == nullptr) return -1;
vector<Node*> vec = {p};
int result = -1;
while (!vec.empty()) {
Node* u = vec.back();
vec.pop_back();
result = max(result, u->index);
for (auto v : u->children) {
if (v) vec.push_back(v);
}
}
return result;
}
};
/**
* Your WordFilter object will be instantiated and called as such:
* WordFilter* obj = new WordFilter(words);
* int param_1 = obj->f(prefix,suffix);
*/