CyberN00b

Untitled

Jan 31st, 2022 (edited)
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include<iostream>
  2. #include<map>
  3. #include<string>
  4. #include<set>
  5.  
  6. using namespace std;
  7.  
  8. void add(string word1, string word2, map<string, pair<int, set<string> > >& s) {
  9.     if (s.contains(word1)) {
  10.         if (s[word1].second.count(word2) == 0)
  11.             s[word1].first++;
  12.     } else {
  13.         pair<int, set<string> > tmp;
  14.         tmp.first = 1;
  15.         tmp.second.insert(word2);
  16.         s.insert({word1, tmp});
  17.     }
  18. }
  19.  
  20. bool check(string word1, string word2, map<string, pair<int, set<string> > >& s) {
  21.     return s[word1].second.contains(word2);
  22. }
  23.  
  24. int main()
  25. {
  26.     map<string, pair<int, set<string> > > synonyms;
  27.     int q = 0;
  28.     cin >> q;
  29.     for (int i = 0; i < q; ++i)
  30.     {
  31.         string operation;
  32.         cin >> operation;
  33.         if (operation == "ADD") {
  34.             string word1, word2;
  35.             cin >> word1 >> word2;
  36.             add(word1, word2, synonyms);
  37.             add(word2, word1, synonyms);
  38.         }
  39.         if (operation == "COUNT") {
  40.             string word;
  41.             cin >> word;
  42.             cout << synonyms[word].first << endl;
  43.         }
  44.         if (operation == "CHECK") {
  45.             string word1, word2;
  46.             cin >> word1 >> word2;
  47.             if (check(word1, word2, synonyms))
  48.                 cout << "YES\n";
  49.             else
  50.                 cout << "NO\n";
  51.         }
  52.     }
  53. }
Add Comment
Please, Sign In to add comment