Advertisement
RobertDeMilo

YB3.4 Проблема двойного включения

Oct 30th, 2023
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.34 KB | None | 0 0
  1. main
  2.  
  3. #include "tests.h"
  4. #include "synonyms.h"
  5.  
  6. #include <iostream>
  7. #include <exception>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12. int main()
  13. {
  14.     TestAll();
  15.  
  16.     int q;
  17.     cin >> q;
  18.     map<string, set<string>>synonyms;
  19.  
  20.     for (int i = 0; i < q; i++)
  21.     {
  22.         string operation_code;
  23.         cin >> operation_code;
  24.  
  25.         if (operation_code == "ADD")
  26.         {
  27.             string first_word, second_word;
  28.             cin >> first_word >> second_word;
  29.  
  30.             ADDSYNONYMS(synonyms, first_word, second_word);
  31.            
  32.         }
  33.         else if (operation_code == "COUNT")
  34.         {
  35.             string word;
  36.             cin >> word;
  37.  
  38.             cout << GETSYNONYMCOUNT(synonyms, word) << endl;
  39.            
  40.         }
  41.         else if (operation_code == "CHECK")
  42.         {
  43.             string first_word, second_word;
  44.             cin >> first_word >> second_word;
  45.  
  46.             if (ARESYNONYMS(synonyms, first_word, second_word))
  47.             {
  48.                 cout << "YES" << endl;
  49.             }
  50.             else
  51.             {
  52.                 cout << "NO" << endl;
  53.             }
  54.         }
  55.     }
  56.  
  57.     return 0;
  58. }
  59. #################################################################################################################
  60. synonyms.h
  61.  
  62. #pragma once
  63.  
  64. #include <string>
  65. #include <set>
  66. #include <map>
  67.  
  68. using namespace std;
  69.  
  70. using SYNONYMS = map<string, set<string>>;
  71.  
  72. //**********************************************************************************************************
  73.  
  74. void ADDSYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word)
  75. {
  76.     synonyms[second_word].insert(first_word);
  77.     synonyms[first_word].insert(second_word);
  78.     //synonyms[first_word].insert(first_word);
  79. }
  80.  
  81. //**********************************************************************************************************
  82.  
  83. size_t GETSYNONYMCOUNT(map<string, set<string>>& synonyms, const string& first_word)
  84. {
  85.     return synonyms[first_word].size();
  86. }
  87.  
  88. //**********************************************************************************************************
  89.  
  90. bool ARESYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word)
  91. {
  92.     return synonyms[first_word].count(second_word) == 1;
  93. }
  94. #################################################################################################################
  95. #pragma once
  96.  
  97. #include <iostream>
  98. #include <sstream>
  99. #include <string>
  100. #include <set>
  101. #include <map>
  102.  
  103. using namespace std;
  104.  
  105. template<class T>
  106. ostream& operator<<(ostream& os, const set<T>& s)
  107. {
  108.     os << "{";
  109.     bool first = true;
  110.  
  111.     for (const auto& x : s)
  112.     {
  113.         if (!first)
  114.         {
  115.             os << ", ";
  116.         }
  117.         first = false;
  118.         os << x;
  119.     }
  120.     return os << "}";
  121. }
  122. //**********************************************************************************************************
  123.  
  124. template <class K, class V>
  125. ostream& operator<<(ostream& os, const map<K, V>& m)
  126. {
  127.     os << "{";
  128.     bool first = true;
  129.     for (const auto& kv : m)
  130.     {
  131.         if (!first)
  132.         {
  133.             os << ", ";
  134.         }
  135.         first = false;
  136.         os << kv.first << ": " << kv.second;
  137.     }
  138.     return os << "}";
  139. }
  140.  
  141. //**********************************************************************************************************
  142.  
  143. template<class T, class U>
  144. void AssertEqual(const T& t, const U& u, const string& hint)
  145. {
  146.     if (t != u)
  147.     {
  148.         ostringstream os;
  149.         os << "Assertion failed: " << t << " != " << u << " Hint: " << hint;
  150.         throw runtime_error(os.str());
  151.     }
  152. }
  153.  
  154. //**********************************************************************************************************
  155.  
  156. void Assert(bool b, const string& hint)
  157. {
  158.     AssertEqual(b, true, hint);
  159. }
  160.  
  161. //**********************************************************************************************************
  162. class TestRunner
  163. {
  164. public:
  165.  
  166.     template <class TestFunc>
  167.     void RunTest(TestFunc func, const string& test_name)
  168.     {
  169.         try
  170.         {
  171.             func();
  172.             cerr << test_name << " OK" << endl;
  173.         }
  174.         catch (runtime_error& e)
  175.         {
  176.             ++fail_count;
  177.             cerr << test_name << " fail: " << e.what() << endl;
  178.         }
  179.  
  180.     }
  181.     ~TestRunner()
  182.     {
  183.         if (fail_count > 0)
  184.         {
  185.             cerr << fail_count << " tests failed. Terminate";
  186.             exit(1);
  187.         }
  188.     }
  189. private:
  190.  
  191.     int fail_count = 0;
  192. };
  193. #################################################################################################################
  194. tests.h
  195.  
  196. #pragma once
  197.  
  198. #include "test_runner.h"
  199. #include "synonyms.h"
  200.  
  201. void TESTADDSYNONYMS()
  202. {
  203.     {
  204.         map<string, set<string>> empty;
  205.         ADDSYNONYMS(empty, "a", "b");
  206.         const map<string, set<string>> expected = { {"a",{"b"}},
  207.                                                     {"b",{"a"}} };
  208.         /*assert(empty == expected);*/
  209.         AssertEqual(empty, expected, "Add to empty");
  210.     }
  211.  
  212.     {
  213.         map<string, set<string>> synonyms = { {"a",{"b"}},
  214.                                               {"b",{"a","c"}},
  215.                                               {"c",{"b"}} };
  216.         ADDSYNONYMS(synonyms, "a", "c");
  217.         const map<string, set<string>> expected = { {"a",{"b","c"}},
  218.                                                     {"b",{"a","c"}},
  219.                                                     {"c",{"b","a"}} };
  220.         /*assert(synonyms == expected);*/
  221.         AssertEqual(synonyms, expected, "Add to non-empty");
  222.  
  223.     }
  224.     /*cout << "TESTADDSYNONYMS OK" << endl;*/
  225. }
  226. //**********************************************************************************************************
  227.  
  228. void TESTCOUNT()
  229. {
  230.     {
  231.         map<string, set<string>> empty;
  232.         /*assert(GETSYNONYMCOUNT(empty, "a") == 0);*/
  233.         AssertEqual(GETSYNONYMCOUNT(empty, "a"), 0u, "count for empty");
  234.     }
  235.  
  236.     {
  237.         map<string, set<string>> synonyms = { {"a",{"b","c"}},
  238.                                               {"b",{"a"}},
  239.                                               {"c",{"a"}} };
  240.  
  241.         /*assert(GETSYNONYMCOUNT(synonyms, "a") == 2);
  242.          assert(GETSYNONYMCOUNT(synonyms, "b") == 1);
  243.          assert(GETSYNONYMCOUNT(synonyms, "z") == 0);*/
  244.         AssertEqual(GETSYNONYMCOUNT(synonyms, "a"), 2u, "count for a");
  245.         AssertEqual(GETSYNONYMCOUNT(synonyms, "b"), 1u, "count for b");
  246.         AssertEqual(GETSYNONYMCOUNT(synonyms, "z"), 0u, "count for z");
  247.     }
  248.     /*cout << "TESTCOUNT OK" << endl;*/
  249. }
  250. //**********************************************************************************************************
  251.  
  252. void TESTARESYNONYMS()
  253. {
  254.     {
  255.         map<string, set<string>> empty;
  256.         /*assert(!ARESYNONYMS(empty, "a", "b"));
  257.          assert(!ARESYNONYMS(empty, "b", "a"));*/
  258.  
  259.          //AssertEqual(ARESYNONYMS(empty, "a", "b"), false, "empty a b");
  260.  
  261.         Assert(!ARESYNONYMS(empty, "a", "b"), "empty a b");
  262.         Assert(!ARESYNONYMS(empty, "a", "b"), "empty b a");
  263.     }
  264.  
  265.     {
  266.         map<string, set<string>> synonyms = { {"a",{"b","c"}},
  267.                                               {"b",{"a"}},
  268.                                               {"c",{"a"}} };
  269.  
  270.         /*assert(ARESYNONYMS(synonyms, "a", "b"));
  271.         assert(ARESYNONYMS(synonyms, "b", "a"));
  272.         assert(ARESYNONYMS(synonyms, "a", "c"));
  273.         assert(ARESYNONYMS(synonyms, "c", "a"));
  274.         assert(!ARESYNONYMS(synonyms, "b", "c"));
  275.         assert(!ARESYNONYMS(synonyms, "c", "b"));*/
  276.         Assert(ARESYNONYMS(synonyms, "a", "b"), " ");
  277.         Assert(ARESYNONYMS(synonyms, "b", "a"), " ");
  278.         Assert(ARESYNONYMS(synonyms, "a", "c"), " ");
  279.         Assert(ARESYNONYMS(synonyms, "c", "a"), " ");
  280.         Assert(!ARESYNONYMS(synonyms, "b", "c"), " ");
  281.         Assert(!ARESYNONYMS(synonyms, "c", "b"), " ");
  282.     }
  283.     /*cout << "TESTARESYNONYMS OK" << endl;*/
  284. }
  285. //**********************************************************************************************************
  286.  
  287. void TestAll()
  288. {
  289.     TestRunner tr;
  290.  
  291.     tr.RunTest(TESTARESYNONYMS, "TESTARESYNONYMS");
  292.     tr.RunTest(TESTADDSYNONYMS, "TESTADDSYNONYMS");
  293.     tr.RunTest(TESTCOUNT, "TESTCOUNT");
  294. }
  295.  
  296. //**********************************************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement