Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://t.me/hitmyfeet канал с музыкой от начинающего программиста.
- #include <cstdlib>
- #include <iomanip>
- #include <iostream>
- #include <map>
- #include <set>
- #include <string>
- #include <vector>
- 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 Container>
- void Print(ostream& out, const Container& container) {
- bool is_first = true;
- for (const auto& element : container) {
- if (!is_first) {
- out << ", "s;
- }
- is_first = false;
- out << element;
- }
- }
- template <typename Element>
- ostream& operator<<(ostream& out, const vector<Element>& container) {
- out << '[';
- Print(out, container);
- out << ']';
- return out;
- }
- template <typename Element>
- ostream& operator<<(ostream& out, const set<Element>& container) {
- out << '{';
- Print(out, container);
- out << '}';
- return out;
- }
- template <typename Key, typename Value>
- ostream& operator<<(ostream& out, const map<Key, Value>& container) {
- out << '{';
- Print(out, container);
- out << '}';
- 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(synonyms_.count(first_word) > 0){
- auto word1 = synonyms_.at(first_word);
- for(const auto& w: word1){
- if(w == second_word)
- 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);
- ASSERT(synonyms.GetSynonymCount("tune"s) == 1);
- ASSERT(synonyms.GetSynonymCount("melody"s) == 1);
- }
- void TestAreSynonyms() {
- Synonyms synonyms;
- synonyms.Add("music", "tune");
- ASSERT(synonyms.AreSynonyms("music", "tune") == true);
- synonyms.Add("melody", "tune");
- ASSERT(synonyms.AreSynonyms("music", "melody") == false);
- synonyms.Add("melody", "tune");
- ASSERT(synonyms.AreSynonyms("melody", "tune") == true);
- }
- 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, second_word;
- command >> first_word >> second_word;
- synonyms.Add(first_word, second_word);
- } else if (action == "COUNT"s) {
- string word;
- command >> word;
- cout << synonyms.GetSynonymCount(word) << endl;
- } 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
Advertisement