Advertisement
mhuman

Untitled

Nov 23rd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int MAXN = 20;
  6. const int MAXLEN = 20;
  7.  
  8. vector <char> SIGMA;
  9. vector <char> N;
  10. vector <string> p[MAXN];
  11. int S;
  12. int EPS = -1;
  13.  
  14. void init_grammar() {
  15.     SIGMA.push_back('a');
  16.     SIGMA.push_back('b');
  17.     SIGMA.push_back('c');
  18.     S = 0;
  19.     N.push_back('S');
  20.     N.push_back('A');
  21.     N.push_back('C');
  22.     N.push_back('D');
  23.     p[0].push_back("A");
  24.     p[1].push_back("C");
  25.     p[1].push_back("AC");
  26.     p[2].push_back("cD");
  27.     p[3].push_back("aDb");
  28.     p[3].push_back("");
  29. }
  30.  
  31. struct situation {
  32.     int v, u;
  33.     size_t pos;
  34.     int i;
  35.  
  36.     situation(int v, int u, size_t pos, int i): v(v), u(u), pos(pos), i(i) {}
  37.  
  38.     bool operator < (const situation& b) const {
  39.         return make_pair(make_pair(v, u), make_pair(pos, i)) < make_pair(make_pair(b.v, b.u), make_pair(b.pos, b.i));
  40.     }
  41. };
  42.  
  43. set <situation> d[MAXLEN];
  44.  
  45. void scan(const string& w, int j) {
  46.     if (j == 0)
  47.         return;
  48.     for (auto v: d[j - 1]) {
  49.         if (v.pos < p[v.v][v.u].size() && p[v.v][v.u][v.pos] == w[j - 1]) {
  50.             situation to = v;
  51.             ++to.pos;
  52.             d[j].insert(to);
  53.         }
  54.     }
  55. }
  56.  
  57. void complete(const string& w, size_t j) {
  58.     for (auto v: d[j]) {
  59.         if (v.pos < p[v.v][v.u].size())
  60.             continue;
  61.         int i = v.i;
  62.         for (auto prev: d[i]) {
  63.             if (prev.pos >= p[prev.v][prev.u].size() || p[prev.v][prev.u][prev.pos] != N[v.v])
  64.                 continue;
  65.             situation to = prev;
  66.             ++to.pos;
  67.             d[j].insert(to);
  68.         }
  69.     }
  70. }
  71.  
  72. void predict(const string& w, int j) {
  73.     for (auto v: d[j]) {
  74.         if (v.pos >= p[v.v][v.u].size() || find(N.begin(), N.end(), p[v.v][v.u][v.pos]) == N.end())
  75.             continue;
  76.         int tov = distance(N.begin(), find(N.begin(), N.end(), p[v.v][v.u][v.pos]));
  77.         for (size_t tou = 0; tou < p[tov].size(); ++tou) {
  78.             d[j].insert(situation(tov, tou, 0, j));
  79.         }
  80.     }
  81. }
  82.  
  83. bool check(string w) {
  84.     for (size_t i = 0; i <= w.size(); ++i) {
  85.         d[i].clear();
  86.     }
  87.     d[0].insert(situation(S, 0, 0, 0));
  88.     for (size_t j = 0; j <= w.size(); ++j) {
  89.         scan(w, j);
  90.         size_t dj_size = -1;
  91.         while (dj_size != d[j].size()) {
  92.             dj_size = d[j].size();
  93.             complete(w, j);
  94.             predict(w, j);
  95.         }
  96.         cout << j << ":" << endl;
  97.         for (auto v: d[j]) {
  98.             cout << N[v.v] << " -> " << p[v.v][v.u] << " " << v.pos << " " << v.i << endl;
  99.         }
  100.     }
  101.  
  102.     situation ok(S, 0, 1, 0);
  103.     return d[w.size()].count(ok);
  104. }
  105.  
  106. int main() {
  107. //  freopen("t.in", "r", stdin);
  108.     init_grammar();
  109.     int tests_cnt;
  110.     cin >> tests_cnt;
  111.     for (int test_num = 0; test_num < tests_cnt; ++test_num) {
  112.         string w;
  113.         cin >> w;
  114.         cout << "test #" << test_num << ":\n";
  115.         int fl = check(w);
  116.         cout << "verdict: ";
  117.         if (fl)
  118.             cout << "YES" << endl;
  119.         else
  120.             cout << "NO" << endl;
  121.     }
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement