-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
37 lines (33 loc) · 890 Bytes
/
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
class Solution {
struct Node {
array<Node*, 26> children = array<Node*, 26>();
int score = 0;
};
void insert(const string& s) {
auto p = root;
for (char c : s) {
int i = c - 'a';
if (p->children[i] == nullptr) p->children[i] = new Node;
p = p->children[i];
++p->score;
}
}
int score(const string& s) {
int result = 0;
auto p = root;
for (char c : s) {
int i = c - 'a';
p = p->children[i];
result += p->score;
}
return result;
}
Node* root = new Node;
public:
vector<int> sumPrefixScores(vector<string>& words) {
for (const string& w : words) insert(w);
vector<int> result;
for (const string& w : words) result.push_back(score(w));
return result;
}
};