achulkov2

Untitled

Sep 24th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <map>
  5. #include <set>
  6.  
  7. using namespace std;
  8. map<string, map<char, set<string>>> go;
  9. map<string, set<string>> eps;
  10. set<string> fin, used;
  11.  
  12. void dfs(const string& from) {
  13.     used.insert(from);
  14.     for (const auto& to : eps[from]) {
  15.         if (used.find(to) == used.end()) {
  16.             dfs(to);
  17.         }
  18.     }
  19. }
  20.  
  21.  
  22. int main() {
  23.     string from, c, to;
  24.     cin >> from;
  25.     while (from != "END") {
  26.         cin >> c >> to;
  27.         if (c != "eps") {
  28.             go[from][c[0]].insert(to);
  29.         } else {
  30.             eps[from].insert(to);
  31.         }
  32.         cin >> from;
  33.     }
  34.     cin >> to;
  35.     while (to != "END") {
  36.         fin.insert(to);
  37.         cin >> to;
  38.     }
  39.     string start, check;
  40.     cin >> start >> check;
  41.     set<string> can = {start};
  42.     for (size_t i = 0; i < check.length(); ++i) {
  43.         used.clear();
  44.         for (const auto& s : can) {
  45.             dfs(s);
  46.         }
  47.         can = used;
  48.         char ch = check[i];
  49.         set<string> nxt;
  50.         for (const auto& s : can) {
  51.             for (const auto& item : go[s][ch]) {
  52.                 nxt.insert(item);
  53.             }
  54.         }
  55.         can = nxt;
  56.         if (can.empty()) {
  57.             cout << "0\n" << i << "\n";
  58.             return 0;
  59.         }
  60.     }
  61.     bool ok = false;
  62.     for (const auto& item : fin) {
  63.         if (can.find(item) != can.end()) {
  64.             ok = true;
  65.             break;
  66.         }
  67.     }
  68.     if (ok) {
  69.         cout << "1";
  70.     } else {
  71.         cout << "0";
  72.     }
  73.     cout << "\n" << check.size() << "\n";
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment