Advertisement
RobertDeMilo

YB2.2 Декомпозиция решения задачи «Синонимы»

Oct 28th, 2023
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<map>
  4. #include<set>
  5.  
  6. using namespace std;
  7.  
  8. void ADDSYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word)
  9. {
  10.     synonyms[second_word].insert(first_word);
  11.     synonyms[first_word].insert(second_word);
  12. }
  13.  
  14. size_t GETSYNONYMCOUNT(map<string, set<string>>& synonyms, const string& first_word)
  15. {
  16.     return synonyms[first_word].size();
  17. }
  18.  
  19. bool ARESYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word)
  20. {
  21.     return synonyms[first_word].count(second_word) == 1;
  22. }
  23.  
  24. int main()
  25. {
  26.     int q;
  27.     cin >> q;
  28.  
  29.     map<string, set<string>> synonyms;
  30.  
  31.     for (int i = 0; i < q; i++)
  32.     {
  33.         string operation_code;
  34.         cin >> operation_code;
  35.  
  36.         if (operation_code == "ADD")
  37.         {
  38.             string first_word, second_word;
  39.             cin >> first_word >> second_word;
  40.  
  41.             ADDSYNONYMS(synonyms, first_word, second_word);
  42.             /*synonyms[second_word].insert(first_word);
  43.             synonyms[first_word].insert(second_word);*/
  44.  
  45.         }
  46.         else if (operation_code == "COUNT")
  47.         {
  48.             string word;
  49.             cin >> word;
  50.             cout << GETSYNONYMCOUNT(synonyms, word) << endl;
  51.             /*cout << synonyms[word].size()<<endl;*/
  52.         }
  53.         else if (operation_code == "CHECK")
  54.         {
  55.             string first_word, second_word;
  56.             cin >> first_word >> second_word;
  57.  
  58.             if (ARESYNONYMS(synonyms, first_word, second_word))
  59.             {
  60.                 cout << "YES" << endl;
  61.             }
  62.             else
  63.             {
  64.                 cout << "NO" << endl;
  65.             }
  66.         }
  67.     }
  68.  
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement