-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0820.cpp
More file actions
43 lines (38 loc) · 1.1 KB
/
Copy path0820.cpp
File metadata and controls
43 lines (38 loc) · 1.1 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
class Trie {
struct Node {
Node(){};
Node *children[27] = {nullptr};
bool &terminate = reinterpret_cast<bool &>(children[0]);
};
Node *trie = new Node();
int length_rec(const Node *node, int len = 0) const {
int res = 1, had = 0;
for (int i = 1; i <= 26; i++) {
if (!node->children[i]) continue;
if (had) res += len + 1;
res += length_rec(node->children[i], len + 1);
had = true;
}
return res;
}
public:
void insert(const string &s) {
Node *crnt = trie;
for (int i = s.size() - 1; i >= 0; i--) {
const int idx = s[i] & 0x1F;
if (!crnt->children[idx]) crnt->children[idx] = new Node();
crnt = crnt->children[idx];
}
crnt->terminate = true;
}
int length(void) const { return length_rec(trie); }
};
class Solution {
public:
int minimumLengthEncoding(const vector<string> &words) {
Trie trie;
for (const string &word : words)
trie.insert(word);
return trie.length();
}
};