Advertisement
Guest User

Untitled

a guest
May 1st, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <map>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. const string END = "END";
  9.  
  10. multimap<string, string> automata;
  11. string end_state;
  12. int end_num = 0;
  13.  
  14. bool rec(const string& input, const string::iterator& it, const string& cur)
  15. {
  16.     if(it == input.end()){
  17.         end_state = cur;
  18.         end_num = it - input.begin();
  19.         return true;
  20.     }
  21.  
  22.     auto s = automata.find(cur + " ");
  23.  
  24.     if(s != automata.end()){
  25.         if(rec(input, it, s->second)){
  26.             return true;
  27.         }
  28.     }
  29.  
  30.     auto range = automata.equal_range(cur + *it);
  31.     for(auto i = range.first; i != range.second; ++i){
  32.         if(rec(input, it+1, i->second)){
  33.             return true;
  34.         }
  35.     }
  36.     if(it - input.begin() > end_num){
  37.         end_num = it - input.begin();
  38.     }
  39.     return false;
  40. }
  41.  
  42.  
  43. int main()
  44. {
  45.     string from, to;
  46.     string ch;
  47.  
  48.     while(cin >> from, from != END){
  49.         cin >> ch >> to;
  50.         if(ch == "eps"){
  51.             from += " ";
  52.         } else {
  53.             from += ch;
  54.         }
  55.         automata.insert(make_pair(from, to));
  56.     }
  57.  
  58.     set<string> end_states;
  59.     string state;
  60.     while(cin >> state, state != END){
  61.         end_states.insert(state);
  62.     }
  63.  
  64.     string cur, input;
  65.     cin >> cur >> input;
  66.  
  67.     bool ok = rec(input, input.begin(), cur);
  68.  
  69.     int result = ok && end_states.find(end_state) != end_states.end();
  70.     cout << result << endl;
  71.     cout << end_num << endl;
  72.  
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement