-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
48 lines (46 loc) · 1.42 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
class Solution {
public:
vector<string> trulyMostPopular(vector<string>& names, vector<string>& synonyms) {
unordered_map<string, vector<string>> g;
unordered_map<string, int> cnt;
for (auto& e : synonyms) {
int i = e.find(',');
string a = e.substr(1, i - 1);
string b = e.substr(i + 1, e.size() - i - 2);
g[a].emplace_back(b);
g[b].emplace_back(a);
}
unordered_set<string> s;
for (auto& e : names) {
int i = e.find('(');
string name = e.substr(0, i);
s.insert(name);
cnt[name] += stoi(e.substr(i + 1, e.size() - i - 2));
}
unordered_set<string> vis;
int freq = 0;
function<string(string)> dfs = [&](string a) -> string {
string res = a;
vis.insert(a);
freq += cnt[a];
for (auto& b : g[a]) {
if (!vis.count(b)) {
string t = dfs(b);
if (t < res) {
res = move(t);
}
}
}
return move(res);
};
vector<string> ans;
for (auto& name : s) {
if (!vis.count(name)) {
freq = 0;
string x = dfs(name);
ans.emplace_back(x + "(" + to_string(freq) + ")");
}
}
return ans;
}
};