chillurbrain

Доп. из 13. Пробуждение.

May 26th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <unordered_set>
  4. #include <string>
  5. #include <sstream>
  6. #include <list>
  7. #include <algorithm>
  8.  
  9. std::unordered_set<int> objSet[1000];
  10. std::unordered_map<std::string, int> dict;
  11. std::vector<std::string> inv;
  12. int nWords = 0;
  13. int nObjSets = 0;
  14.  
  15. int main()
  16. {
  17.     std::unordered_map<std::string, std::unordered_set<int>*> strToObj;
  18.     std::ios::sync_with_stdio(false);
  19.  
  20.     std::string input;
  21.     std::getline(std::cin, input);
  22.     int N = std::stoi(input);
  23.     while (N--)
  24.     {
  25.         std::getline(std::cin, input);
  26.         std::stringstream ss(input);
  27.         std::string str;
  28.         ss >> str;
  29.         str.pop_back();
  30.         if (strToObj.find(str) == strToObj.end())
  31.             strToObj.insert({ str, objSet + nObjSets++ });
  32.         while (ss >> str)
  33.         {
  34.             auto it = dict.insert({ str, nWords }).first;
  35.             inv.push_back(str);
  36.             nWords++;
  37.             objSet[nObjSets - 1].insert(it->second);
  38.         }
  39.     }
  40.  
  41.     std::getline(std::cin, input);
  42.     int M = std::stoi(input);
  43.     while (M--)
  44.     {
  45.         std::getline(std::cin, input);
  46.         std::stringstream ss(input);
  47.         std::string str;
  48.         ss >> str;
  49.         std::list<int> list(strToObj[str]->begin(), strToObj[str]->end());
  50.         while (ss >> str)
  51.         {
  52.             auto set = strToObj[str];
  53.             for (auto it = list.begin(); it != list.end();)
  54.             if (set->find(*it) == set->end())
  55.                 it = list.erase(it);
  56.             else
  57.                 it++;
  58.         }
  59.         std::vector<std::string> ans;
  60.         for (auto it = list.begin(); it != list.end(); it++)
  61.             ans.push_back(inv[*it]);
  62.         std::sort(ans.begin(), ans.end());
  63.         if (ans.size())
  64.         for (auto it = ans.begin(); it < ans.end(); it++)
  65.             std::cout << *it << " ";
  66.         else
  67.             std::cout << "No solution.";
  68.         std::cout << std::endl;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment