Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<map>
- #include<string>
- #include<set>
- using namespace std;
- void add(string word1, string word2, map<string, pair<int, set<string> > >& s) {
- if (s.contains(word1)) {
- if (s[word1].second.count(word2) == 0)
- s[word1].first++;
- } else {
- pair<int, set<string> > tmp;
- tmp.first = 1;
- tmp.second.insert(word2);
- s.insert({word1, tmp});
- }
- }
- bool check(string word1, string word2, map<string, pair<int, set<string> > >& s) {
- return s[word1].second.contains(word2);
- }
- int main()
- {
- map<string, pair<int, set<string> > > synonyms;
- int q = 0;
- cin >> q;
- for (int i = 0; i < q; ++i)
- {
- string operation;
- cin >> operation;
- if (operation == "ADD") {
- string word1, word2;
- cin >> word1 >> word2;
- add(word1, word2, synonyms);
- add(word2, word1, synonyms);
- }
- if (operation == "COUNT") {
- string word;
- cin >> word;
- cout << synonyms[word].first << endl;
- }
- if (operation == "CHECK") {
- string word1, word2;
- cin >> word1 >> word2;
- if (check(word1, word2, synonyms))
- cout << "YES\n";
- else
- cout << "NO\n";
- }
- }
- }
Add Comment
Please, Sign In to add comment