nochnoikoshmar1

Untitled

Mar 16th, 2023
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.72 KB | None | 0 0
  1. #include <cmath>
  2. #include <map>
  3. #include <string>
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <set>
  7. #include <utility>
  8. #include <vector>
  9. #include <numeric>
  10. #include <chrono>
  11. #include <type_traits>
  12. #include <cassert>
  13. #include <sstream>
  14. #include <unordered_map>
  15. using namespace std;
  16.  
  17. //template <typename First, typename Second>
  18. //ostream &operator<<(ostream &out, const pair<First, Second> &p){
  19. //    return out <<  p.first << ": "s << p.second ;
  20. //}
  21. template <typename Element, typename T>
  22. void Print(ostream &out, const map<Element, T> &container){
  23.     int count = 1;
  24.     for(const auto &[element, age] : container){
  25.         out << element << ": " << age;
  26.         if(container.size() > count){
  27.             out << ", "s;
  28.             ++count;
  29.         }
  30.     }
  31. }
  32. template <typename Element>
  33. void Print(ostream &out, const Element &container){
  34.     bool is_map = true;
  35.     for(const auto &element : container){
  36.         if(!is_map)
  37.             out << ", "s;        
  38.         is_map = false;
  39.         out << element;
  40.     }
  41. }
  42. template <typename Element>
  43. ostream &operator<<(ostream &out, const vector<Element> &container){
  44.     out << "["s;
  45.     Print(out, container);
  46.     out << "]"s;
  47.     return out;
  48. }
  49. template <typename Element>
  50. ostream &operator<<(ostream &out, const set<Element> &container){
  51.     out << "{"s;
  52.     Print(out, container);
  53.     out << "}"s;
  54.     return out;
  55. }
  56. template <typename Element, typename T>
  57. ostream &operator<<(ostream &out, const map<Element,T> &container){    
  58.     out << "{"s;
  59.     Print(out, container);
  60.     out << "}"s;
  61.     return out;
  62. }
  63.  
  64. template <typename T, typename U>
  65. void AssertEqualImpl(const T &t, const U &u, const string &t_str, const string &u_str, const string &file, const string &func, unsigned line, const string &hint = ""){
  66.     if(t != u){
  67.         cout << boolalpha;
  68.         cout << file << "("s << line << "): "s << func << ": "s;
  69.         cout << "ASSERT_EQUAL("s << t_str << ", "s << u_str << ") failed: "s;
  70.         cout << t << " != "s << u << "."s;
  71.         if(!hint.empty()){
  72.             cout << " Hint: "s << hint;
  73.         }
  74.         cout << endl;
  75.         abort();
  76.     }
  77. }
  78. #define ASSERT_EQUAL(a, b) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, ""s)
  79. #define ASSERT_EQUAL_HINT(a, b, hint) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, (hint))
  80.  
  81. void AssertImpl(bool value, const string &expr_str, const string &file, const string &func, unsigned line, const string &hint = ""){
  82.     if(!value){
  83.         cout << file << "("s << line << "): "s << func << ": "s;
  84.         cout << "ASSERT("s << expr_str << ") failed."s;
  85.         if(!hint.empty()){
  86.             cout << " Hint: "s << hint;
  87.         }
  88.         cout << endl;
  89.         abort();
  90.     }
  91. }
  92. #define ASSERT(expr) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, ""s)
  93. #define ASSERT_HINT(expr, hint) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, (hint))
  94.  
  95.  
  96. class Synonyms{
  97. public:
  98.     void Add(const string &first_word, const string &second_word){
  99.         synonyms_[first_word].insert(second_word);
  100.         synonyms_[second_word].insert(first_word);
  101.     }
  102.  
  103.     size_t GetSynonymCount(const string &word) const{
  104.         if(synonyms_.count(word) != 0){
  105.             return synonyms_.at(word).size();
  106.         }
  107.         return 0;
  108.     }
  109.  
  110.     bool AreSynonyms(const string &first_word, const string &second_word) const{
  111.  
  112.         if(!first_word.empty() && !second_word.empty() && first_word != second_word){
  113.             if(count(synonyms_.at(first_word).begin(), synonyms_.at(first_word).end(), second_word) > 0){
  114.                 return true;
  115.             } else{
  116.                 return false;
  117.             }
  118.             /*if(synonyms_.count(first_word) == 1){
  119.                 if(synonyms_.count(second_word) == 1){
  120.                     return false;
  121.                 }
  122.             }else{
  123.                 return true;
  124.             }*/
  125.             /*if(synonyms_.count(first_word) != 0){
  126.                 if(synonyms_.at(first_word).count(second_word) > 0){
  127.                     return true;
  128.                 }
  129.             }*/
  130.         }
  131.         return false;
  132.     }
  133.  
  134. private:
  135.     map<string, set<string>> synonyms_;
  136. };
  137.  
  138. void TestAddingSynonymsIncreasesTheirCount(){
  139.     Synonyms synonyms;
  140.     ASSERT(synonyms.GetSynonymCount("music"s) == 0);
  141.     ASSERT(synonyms.GetSynonymCount("melody"s) == 0);
  142.  
  143.     synonyms.Add("music"s, "melody"s);
  144.     // Два слова являются синонимами друг друга
  145.     ASSERT(synonyms.GetSynonymCount("music"s) == 1);
  146.     ASSERT(synonyms.GetSynonymCount("melody"s) == 1);
  147.  
  148.     synonyms.Add("music"s, "tune"s);
  149.     ASSERT(synonyms.GetSynonymCount("music"s) == 2);
  150.  
  151.     // Отношение синонимичности в нашей постановке задачи не является транзитивным.
  152.     // Поэтому слова "tune" и "melody" синонимами друг друга не являются,
  153.     // несмотря на то что оба они являются синонимами слова "music".
  154.     ASSERT(synonyms.GetSynonymCount("tune"s) == 1);
  155.     ASSERT(synonyms.GetSynonymCount("melody"s) == 1);
  156. }
  157. void TestAreSynonyms(){
  158.     Synonyms synonyms;
  159.  
  160.     ASSERT(synonyms.AreSynonyms("music"s, "melody"s) == 1);
  161.     ASSERT(synonyms.AreSynonyms("music"s, "music"s) == 0);
  162.     ASSERT(synonyms.AreSynonyms("music"s, "tune"s) == 0);
  163. }
  164. void TestSynonyms(){
  165.     TestAddingSynonymsIncreasesTheirCount();
  166.     TestAreSynonyms();
  167. }
  168.  
  169. int main(){
  170.     TestSynonyms();
  171.  
  172.     Synonyms synonyms;
  173.     string line;
  174.  
  175.     while(getline(cin, line)){
  176.         istringstream command(line);
  177.         string action;
  178.         command >> action;
  179.  
  180.         if(action == "ADD"s){
  181.             string first_word/* = "melody"s*/, second_word/* = "music"s*/;
  182.             command >> first_word >> second_word;
  183.             synonyms.Add(first_word, second_word);
  184.  
  185.         } else{
  186.             if(action == "COUNT"s){
  187.                 string word /*= "melody"s*/;
  188.                 command >> word;
  189.                 //cout << synonyms.GetSynonymCount(word) << endl;
  190.                 ASSERT(synonyms.GetSynonymCount(word));
  191.             } else{
  192.                 if(action == "CHECK"s){
  193.                     string first_word, second_word;
  194.                     command >> first_word >> second_word;
  195.  
  196.                     if(synonyms.AreSynonyms(first_word, second_word)){
  197.                         cout << "YES"s << endl;
  198.                     } else{
  199.                         cout << "NO"s << endl;
  200.                     }
  201.                 } else{
  202.                     if(action == "EXIT"s){
  203.                         break;
  204.                     }
  205.                 }
  206.             }
  207.         }
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment