-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathtrendingtopic.cc
More file actions
51 lines (48 loc) · 1.19 KB
/
Copy pathtrendingtopic.cc
File metadata and controls
51 lines (48 loc) · 1.19 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
44
45
46
47
48
49
50
51
// https://open.kattis.com/problems/trendingtopic
#include <bits/stdc++.h>
using namespace std;
using is = tuple<int, string>;
int f[20001];
int main() {
unordered_map<string, int> m;
vector<string> mi;
queue<tuple<int, int>> q;
int i = 0, d = 0;
while (!cin.eof()) {
string s;
cin >> s;
if (s.size() < 4 || s == "</text>") continue;
if (s == "<text>") {
d++;
while (!q.empty()) {
int k, t;
tie(k, t) = q.front();
if (k > d - 7) break;
f[t]--;
q.pop();
}
} else if (s == "<top") {
int n;
cin >> n >> s;
cout << "<top " << n << ">\n";
if (n > i) n = i;
vector<is> a(i);
for (int k = 0; k < i; k++)
a[k] = {f[k], mi[k]};
sort(a.begin(), a.end(), [](is &a, is&b) { return get<0>(a) > get<0>(b) || (get<0>(a) == get<0>(b) && get<1>(a) < get<1>(b)); });
int l = get<0>(a[n - 1]);
for (is &x : a) {
int k; string s;
tie(k, s) = x;
if (k < l) break;
cout << s << " " << k << "\n";
}
cout << "</top>\n";
} else {
if (!m.count(s)) m[s] = i++, mi.push_back(s);
int k = m[s];
f[k]++;
q.push({d, k});
}
}
}