Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include <utility>
  2.  
  3. #include "bits/stdc++.h"
  4.  
  5. using namespace std;
  6.  
  7. class Word {
  8. public:
  9.     Word(int cnt, string word) : cnt(cnt), word(std::move(word)) {}
  10.     string word;
  11.     int cnt;
  12. };
  13.  
  14. int compareInt(int x, int y) {
  15.     if (x == y) { return 0; }
  16.     if (x > y) { return 1; }
  17.     return -1;
  18. }
  19.  
  20. bool predicate(Word word1, Word word2) {
  21.     int key = compareInt(word1.cnt, word2.cnt);
  22.     if (key == 0) {
  23.         key = word2.word.compare(word1.word);
  24.     }
  25.     return key >= 0;
  26. }
  27.  
  28. int main() {
  29.  
  30.     int n;
  31.     cin >> n;
  32.     cin.ignore();
  33.  
  34.     while (n) {
  35.  
  36.         map<string, pair<int, unordered_set<string>>> map;
  37.         set<string> people;
  38.  
  39.         string word;
  40.         string name;
  41.         bool isFirst = true;
  42.  
  43.         for (int i = 0; i < n; i++) {
  44.             string line;
  45.             getline(cin, line);
  46.  
  47.             stringstream ss(line);
  48.             while (ss >> word) {
  49.                 if (isFirst) {
  50.                     name = word;
  51.                     isFirst = false;
  52.                     people.insert(name);
  53.                 } else {
  54.                     if (!map.count(word)) {
  55.                         pair<int, unordered_set<string>> pair;
  56.                         pair.first = 1;
  57.                         pair.second.insert(name);
  58.                         map.insert(std::pair<string, std::pair<int, unordered_set<string>>>(word, pair));
  59.                     } else {
  60.                         map.operator[](word).first++;
  61.                         map.operator[](word).second.insert(name);
  62.                     }
  63.                 }
  64.             }
  65.             isFirst = true;
  66.         }
  67.  
  68.         vector<Word> results;
  69.         for (const auto &x : map) {
  70.             if (x.second.second.size() == people.size()) {
  71.                 results.emplace_back(x.second.first, x.first);
  72.             }
  73.         }
  74.  
  75.         if(results.empty()){
  76.             cout << "ALL CLEAR" << endl;
  77.         } else {
  78.             sort(results.begin(), results.end(), predicate);
  79.             for (const auto &x : results) {
  80.                 cout << x.word << endl;
  81.             }
  82.         }
  83.  
  84.         cin >> n;
  85.         cin.ignore();
  86.     }
  87.  
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement