Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<exception>
- #include<sstream>
- #include<string>
- #include<map>
- #include<set>
- using namespace std;
- using SYNONYMS = map<string, set<string>>;
- //**********************************************************************************************************
- void ADDSYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word)
- {
- synonyms[second_word].insert(first_word);
- synonyms[first_word].insert(second_word);
- //synonyms[first_word].insert(first_word);
- }
- //**********************************************************************************************************
- size_t GETSYNONYMCOUNT(map<string, set<string>>& synonyms, const string& first_word)
- {
- return synonyms[first_word].size();
- }
- //**********************************************************************************************************
- bool ARESYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word)
- {
- return synonyms[first_word].count(second_word) == 1;
- }
- //**********************************************************************************************************
- //##########################################################################################################
- template<class T>
- ostream& operator<<(ostream& os, const set<T>& s)
- {
- os << "{";
- bool first = true;
- for (const auto& x : s)
- {
- if (!first)
- {
- os << ", ";
- }
- first = false;
- os << x;
- }
- return os << "}";
- }
- template <class K, class V>
- ostream& operator<<(ostream& os, const map<K, V>& m)
- {
- os << "{";
- bool first = true;
- for (const auto& kv : m)
- {
- if (!first)
- {
- os << ", ";
- }
- first = false;
- os << kv.first << ": " << kv.second;
- }
- return os << "}";
- }
- //##########################################################################################################
- //template<class T, class U>
- //void AssertEqual(const T& t, const U& u, const string& hint)
- //{
- // if (t != u)
- // {
- // ostringstream os;
- // os << "Assertion failed: " << t << " != " << u << " Hint: " << hint;
- // throw runtime_error(os.str());
- // }
- //}
- template <typename T, typename U>
- void AssertEqual(const T& t, const U& u, const string& hint)
- {
- if (t != u) {
- // Включаем режим вывода значений типа bool в виде true и false
- cout << boolalpha;
- cout << "Assertion failed: "s << t << " != "s << u << "."s;
- if (!hint.empty()) {
- cout << " Hint: "s << hint;
- }
- cout << endl;
- abort();
- }
- }
- void Assert(bool b, const string& hint)
- {
- AssertEqual(b, true, hint);
- }
- //##########################################################################################################
- //**********************************************************************************************************
- void TESTADDSYNONYMS()
- {
- {
- map<string, set<string>> empty;
- ADDSYNONYMS(empty, "a", "b");
- const map<string, set<string>> expected = { {"a",{"b"}},
- {"b",{"a"}} };
- /*assert(empty == expected);*/
- AssertEqual(empty, expected, "Add to empty");
- }
- {
- map<string, set<string>> synonyms = { {"a",{"b"}},
- {"b",{"a","c"}},
- {"c",{"b"}} };
- ADDSYNONYMS(synonyms, "a", "c");
- const map<string, set<string>> expected = { {"a",{"b","c"}},
- {"b",{"a","c"}},
- {"c",{"b","a"}} };
- /*assert(synonyms == expected);*/
- AssertEqual(synonyms, expected, "Add to non-empty");
- }
- cout << "TESTADDSYNONYMS OK" << endl;
- }
- //**********************************************************************************************************
- void TESTCOUNT()
- {
- {
- map<string, set<string>> empty;
- /*assert(GETSYNONYMCOUNT(empty, "a") == 0);*/
- AssertEqual(GETSYNONYMCOUNT(empty, "a"), 0u, "count for empty");
- }
- {
- map<string, set<string>> synonyms = { {"a",{"b","c"}},
- {"b",{"a"}},
- {"c",{"a"}} };
- /*assert(GETSYNONYMCOUNT(synonyms, "a") == 2);
- assert(GETSYNONYMCOUNT(synonyms, "b") == 1);
- assert(GETSYNONYMCOUNT(synonyms, "z") == 0);*/
- AssertEqual(GETSYNONYMCOUNT(synonyms, "a"), 2u, "count for a");
- AssertEqual(GETSYNONYMCOUNT(synonyms, "b"), 1u, "count for b");
- AssertEqual(GETSYNONYMCOUNT(synonyms, "z"), 0u, "count for z");
- }
- cout << "TESTCOUNT OK" << endl;
- }
- //**********************************************************************************************************
- void TESTARESYNONYMS()
- {
- {
- map<string, set<string>> empty;
- /*assert(!ARESYNONYMS(empty, "a", "b"));
- assert(!ARESYNONYMS(empty, "b", "a"));*/
- //AssertEqual(ARESYNONYMS(empty, "a", "b"), false, "empty a b");
- Assert(!ARESYNONYMS(empty, "a", "b"), "empty a b");
- Assert(!ARESYNONYMS(empty, "a", "b"), "empty b a");
- }
- {
- map<string, set<string>> synonyms = { {"a",{"b","c"}},
- {"b",{"a"}},
- {"c",{"a"}} };
- /*assert(ARESYNONYMS(synonyms, "a", "b"));
- assert(ARESYNONYMS(synonyms, "b", "a"));
- assert(ARESYNONYMS(synonyms, "a", "c"));
- assert(ARESYNONYMS(synonyms, "c", "a"));
- assert(!ARESYNONYMS(synonyms, "b", "c"));
- assert(!ARESYNONYMS(synonyms, "c", "b"));*/
- Assert(ARESYNONYMS(synonyms, "a", "b"), " ");
- Assert(ARESYNONYMS(synonyms, "b", "a"), " ");
- Assert(ARESYNONYMS(synonyms, "a", "c"), " ");
- Assert(ARESYNONYMS(synonyms, "c", "a"), " ");
- Assert(!ARESYNONYMS(synonyms, "b", "c"), " ");
- Assert(!ARESYNONYMS(synonyms, "c", "b"), " ");
- }
- cout << "TESTARESYNONYMS OK" << endl;
- }
- //**********************************************************************************************************
- void TESTALL()
- {
- TESTCOUNT();
- TESTADDSYNONYMS();
- TESTARESYNONYMS();
- }
- //**********************************************************************************************************
- int main()
- {
- TESTALL();
- return 0;
- //int q;
- //cin >> q;
- //map<string, set<string>>synonyms;
- //for (int i = 0; i < q; i++)
- //{
- // string operation_code;
- // cin >> operation_code;
- // if (operation_code == "ADD")
- // {
- // string first_word, second_word;
- // cin >> first_word >> second_word;
- // ADDSYNONYMS(synonyms, first_word, second_word);
- // /*synonyms[second_word].insert(first_word);
- // synonyms[first_word].insert(second_word);*/
- // }
- // else if (operation_code == "COUNT")
- // {
- // string word;
- // cin >> word;
- // cout << GETSYNONYMCOUNT(synonyms, word) << endl;
- // /*cout << synonyms[word].size()<<endl;*/
- // }
- // else if (operation_code == "CHECK")
- // {
- // string first_word, second_word;
- // cin >> first_word >> second_word;
- // if (ARESYNONYMS(synonyms, first_word, second_word))
- // {
- // cout << "YES" << endl;
- // }
- // else
- // {
- // cout << "NO" << endl;
- // }
- // }
- //}
- //return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement