Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <map>
- #include <string>
- #include <algorithm>
- #include <iostream>
- #include <set>
- #include <utility>
- #include <vector>
- #include <numeric>
- #include <chrono>
- #include <type_traits>
- #include <cassert>
- #include <sstream>
- #include <unordered_map>
- using namespace std;
- //template <typename First, typename Second>
- //ostream &operator<<(ostream &out, const pair<First, Second> &p){
- // return out << p.first << ": "s << p.second ;
- //}
- template <typename Element, typename T>
- void Print(ostream &out, const map<Element, T> &container){
- int count = 1;
- for(const auto &[element, age] : container){
- out << element << ": " << age;
- if(container.size() > count){
- out << ", "s;
- ++count;
- }
- }
- }
- template <typename Element>
- void Print(ostream &out, const Element &container){
- bool is_map = true;
- for(const auto &element : container){
- if(!is_map)
- out << ", "s;
- is_map = false;
- out << element;
- }
- }
- template <typename Element>
- ostream &operator<<(ostream &out, const vector<Element> &container){
- out << "["s;
- Print(out, container);
- out << "]"s;
- return out;
- }
- template <typename Element>
- ostream &operator<<(ostream &out, const set<Element> &container){
- out << "{"s;
- Print(out, container);
- out << "}"s;
- return out;
- }
- template <typename Element, typename T>
- ostream &operator<<(ostream &out, const map<Element,T> &container){
- out << "{"s;
- Print(out, container);
- out << "}"s;
- return out;
- }
- template <typename T, typename U>
- 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 = ""){
- if(t != u){
- cout << boolalpha;
- cout << file << "("s << line << "): "s << func << ": "s;
- cout << "ASSERT_EQUAL("s << t_str << ", "s << u_str << ") failed: "s;
- cout << t << " != "s << u << "."s;
- if(!hint.empty()){
- cout << " Hint: "s << hint;
- }
- cout << endl;
- abort();
- }
- }
- #define ASSERT_EQUAL(a, b) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, ""s)
- #define ASSERT_EQUAL_HINT(a, b, hint) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, (hint))
- void AssertImpl(bool value, const string &expr_str, const string &file, const string &func, unsigned line, const string &hint = ""){
- if(!value){
- cout << file << "("s << line << "): "s << func << ": "s;
- cout << "ASSERT("s << expr_str << ") failed."s;
- if(!hint.empty()){
- cout << " Hint: "s << hint;
- }
- cout << endl;
- abort();
- }
- }
- #define ASSERT(expr) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, ""s)
- #define ASSERT_HINT(expr, hint) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, (hint))
- class Synonyms{
- public:
- void Add(const string &first_word, const string &second_word){
- synonyms_[first_word].insert(second_word);
- synonyms_[second_word].insert(first_word);
- }
- size_t GetSynonymCount(const string &word) const{
- if(synonyms_.count(word) != 0){
- return synonyms_.at(word).size();
- }
- return 0;
- }
- bool AreSynonyms(const string &first_word, const string &second_word) const{
- if(!first_word.empty() && !second_word.empty() && first_word != second_word){
- if(count(synonyms_.at(first_word).begin(), synonyms_.at(first_word).end(), second_word) > 0){
- return true;
- } else{
- return false;
- }
- /*if(synonyms_.count(first_word) == 1){
- if(synonyms_.count(second_word) == 1){
- return false;
- }
- }else{
- return true;
- }*/
- /*if(synonyms_.count(first_word) != 0){
- if(synonyms_.at(first_word).count(second_word) > 0){
- return true;
- }
- }*/
- }
- return false;
- }
- private:
- map<string, set<string>> synonyms_;
- };
- void TestAddingSynonymsIncreasesTheirCount(){
- Synonyms synonyms;
- ASSERT(synonyms.GetSynonymCount("music"s) == 0);
- ASSERT(synonyms.GetSynonymCount("melody"s) == 0);
- synonyms.Add("music"s, "melody"s);
- // Два слова являются синонимами друг друга
- ASSERT(synonyms.GetSynonymCount("music"s) == 1);
- ASSERT(synonyms.GetSynonymCount("melody"s) == 1);
- synonyms.Add("music"s, "tune"s);
- ASSERT(synonyms.GetSynonymCount("music"s) == 2);
- // Отношение синонимичности в нашей постановке задачи не является транзитивным.
- // Поэтому слова "tune" и "melody" синонимами друг друга не являются,
- // несмотря на то что оба они являются синонимами слова "music".
- ASSERT(synonyms.GetSynonymCount("tune"s) == 1);
- ASSERT(synonyms.GetSynonymCount("melody"s) == 1);
- }
- void TestAreSynonyms(){
- Synonyms synonyms;
- ASSERT(synonyms.AreSynonyms("music"s, "melody"s) == 1);
- ASSERT(synonyms.AreSynonyms("music"s, "music"s) == 0);
- ASSERT(synonyms.AreSynonyms("music"s, "tune"s) == 0);
- }
- void TestSynonyms(){
- TestAddingSynonymsIncreasesTheirCount();
- TestAreSynonyms();
- }
- int main(){
- TestSynonyms();
- Synonyms synonyms;
- string line;
- while(getline(cin, line)){
- istringstream command(line);
- string action;
- command >> action;
- if(action == "ADD"s){
- string first_word/* = "melody"s*/, second_word/* = "music"s*/;
- command >> first_word >> second_word;
- synonyms.Add(first_word, second_word);
- } else{
- if(action == "COUNT"s){
- string word /*= "melody"s*/;
- command >> word;
- //cout << synonyms.GetSynonymCount(word) << endl;
- ASSERT(synonyms.GetSynonymCount(word));
- } else{
- if(action == "CHECK"s){
- string first_word, second_word;
- command >> first_word >> second_word;
- if(synonyms.AreSynonyms(first_word, second_word)){
- cout << "YES"s << endl;
- } else{
- cout << "NO"s << endl;
- }
- } else{
- if(action == "EXIT"s){
- break;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment