Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cassert>
- #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;
- }
- **********************************************************************************************************
- void TESTADDSYNONYMS()
- {
- {
- map<string, set<string>> empty;
- ADDSYNONYMS(empty, "a", "b");
- const map<string, set<string>> expected = { {"a",{"b"}},
- {"b",{"a"}} };
- assert(empty == expected);
- }
- {
- 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);
- }
- cout << "TESTADDSYNONYMS OK" << endl;
- }
- **********************************************************************************************************
- void TESTCOUNT()
- {
- {
- map<string, set<string>> empty;
- assert(GETSYNONYMCOUNT(empty, "a") == 0);
- }
- {
- 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);
- }
- cout << "TESTCOUNT OK" << endl;
- }
- **********************************************************************************************************
- void TESTARESYNONYMS()
- {
- {
- map<string, set<string>> empty;
- assert(!ARESYNONYMS(empty, "a", "b"));
- assert(!ARESYNONYMS(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"));
- }
- cout << "TESTARESYNONYMS OK" << endl;
- }
- **********************************************************************************************************
- void TESTALL()
- {
- TESTCOUNT();
- TESTARESYNONYMS();
- TESTADDSYNONYMS();
- }
- **********************************************************************************************************
- 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