fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define Samurai ios_base::sync_with_stdio(false), cout.tie(NULL), cin.tie(NULL);
  5. #define pr_g priority_queue<pair<ll,int>, vector<pair<ll,int>>,greater<pair<ll,int>>>
  6. int dx [] = {0, 0, 1, -1, 1, 1, -1, -1};
  7. int dy [] = {-1, 1, 0, 0, -1, 1, 1, -1};
  8. char dir [] = {'>', '<', '^', 'v'};
  9. int Lx[] = {2, 2, -2, -2, 1, 1, -1, -1};
  10. int Ly[] = {1, -1, 1, -1, 2, -2, 2, -2};
  11. const double PI = acos(-1.0);
  12. #define el '\n'
  13. const ll mod = 1e9 + 7, N = 2e5 + 5, OO = 0x3f3f3f3f;
  14.  
  15. bool cmp(pair<int, string>&a, pair<int, string>&b) {
  16. if (a.first != b.first)
  17. return a.first > b.first;
  18. else
  19. return a.second < b.second;
  20. }
  21.  
  22. void solve() {
  23. int n; cin >> n;
  24. cin.ignore();
  25. set<string> s;
  26. vector<string> v;
  27. map<string, int> mp;
  28. for (int i = 0; i < n; i++) {
  29. string x;
  30. getline(cin, x);
  31. if (isupper(x[0])) {
  32. for (auto &j : s) {
  33. v.push_back(j);
  34. mp[j]++;
  35. }
  36. s.clear();
  37. v.push_back(x);
  38. }
  39. else s.insert(x);
  40. }
  41. for (auto &j : s) {
  42. v.push_back(j);
  43. mp[j]++;
  44. }
  45. vector<pair<int, string>> ans;
  46. for (int i = 0; i < v.size(); i++) {
  47. string x = v[i];
  48. int c = 0;
  49. while (i + 1 < v.size() && islower(v[i + 1][0])) {
  50. i++;
  51. if (mp[v[i]] == 1)
  52. c++;
  53. }
  54. ans.push_back({c, x});
  55. }
  56. sort(ans.begin(), ans.end(), cmp);
  57. for (int i = 0; i < ans.size(); i++)
  58. cout << ans[i].second << ' ' << ans[i].first << el;
  59. }
  60.  
  61. int main() { Samurai
  62. freopen("coaches.in", "r", stdin);
  63. int _t = 1; cin >> _t;
  64. cin.ignore();
  65. for (int i = 1; i <= _t; i++){
  66. solve();
  67. }
  68. return 0;
  69. }
Success #stdin #stdout 0.01s 5328KB
stdin
Standard input is empty
stdout
Standard output is empty