forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
61 lines (55 loc) · 1.5 KB
/
Solution.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
class Trie {
public:
Trie()
: cnt(0) {
fill(children.begin(), children.end(), nullptr);
}
void insert(const string& w) {
Trie* node = this;
for (char c : w) {
int idx = c - 'a';
if (node->children[idx] == nullptr) {
node->children[idx] = new Trie();
}
node = node->children[idx];
++node->cnt;
}
}
int search(const string& w) {
Trie* node = this;
int ans = 0;
for (char c : w) {
++ans;
int idx = c - 'a';
node = node->children[idx];
if (node->cnt == 1) {
return ans;
}
}
return w.size();
}
private:
array<Trie*, 26> children;
int cnt;
};
class Solution {
public:
vector<string> wordsAbbreviation(vector<string>& words) {
map<pair<int, int>, Trie*> tries;
for (const auto& w : words) {
pair<int, int> key = {static_cast<int>(w.size()), w.back() - 'a'};
if (tries.find(key) == tries.end()) {
tries[key] = new Trie();
}
tries[key]->insert(w);
}
vector<string> ans;
for (const auto& w : words) {
int m = w.size();
pair<int, int> key = {m, w.back() - 'a'};
int cnt = tries[key]->search(w);
ans.push_back((cnt + 2 >= m) ? w : w.substr(0, cnt) + to_string(m - cnt - 1) + w.back());
}
return ans;
}
};