Advertisement
he_obviously

aho-korasik

Oct 28th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <limits.h>
  5. #include <map>
  6. #include <unordered_map>
  7. #include <set>
  8. #include <unordered_set>
  9. #include <string>
  10. #include <algorithm>
  11. #include <iomanip>
  12. #include <random>
  13. #include <time.h>
  14. #include <bitset>
  15. #include <queue>
  16. #include <deque>
  17.  
  18. using namespace std;
  19.  
  20. typedef long long ll;
  21. typedef unsigned long long ull;
  22. typedef long double ld;
  23.  
  24. #define all(x) (x).begin(),(x).end()
  25. #define sz(x) (int)x.size()
  26.  
  27. const int k = 26, MAXN = 100001;
  28.  
  29. struct node {
  30.     map<char, node*> to, go;
  31.     node* link;
  32.     node* parent;
  33.     char parent_char;
  34.     bool terminal = 0;
  35.     node() {}
  36.     node(char _pch, node* _p) {
  37.         parent_char = _pch;
  38.         parent = _p;
  39.     }
  40. };
  41.  
  42. node* insert(string& s, node* root) {
  43.     node* v = root;
  44.     for (char c : s) {
  45.         if (!v->to[c]) {
  46.             v->to[c] = new node(c, v);
  47.         }
  48.         v = v->to[c];
  49.     }
  50.     v->terminal = 1;
  51.     return v;
  52. }
  53.  
  54. node* go(node* v, char c, node* root) {
  55.     if (!v->go[c]) {
  56.         if (v->to[c]) {
  57.             v->go[c] = v->to[c];
  58.         }
  59.         else if (v == root) {
  60.             v->go[c] = root;
  61.         }
  62.         else {
  63.             v->go[c] = go(v->link, c, root);
  64.         }
  65.     }
  66.     return v->go[c];
  67. }
  68.  
  69. int main() {
  70.  
  71.     ios_base::sync_with_stdio(0);
  72.     cin.tie(0); cout.tie(0);
  73.  
  74.     node* root = new node();
  75.  
  76.     string t;
  77.     cin >> t;
  78.  
  79.     int n;
  80.     cin >> n;
  81.  
  82.     vector<string> vec(n);
  83.     map<node*, string> a;
  84.     map<string, bool> b;
  85.  
  86.     for (int i = 0; i < n; ++i) {
  87.         string s;
  88.         cin >> s;
  89.         node* cur = insert(s, root);
  90.         vec[i] = s;
  91.         a[cur] = s;
  92.         b[s] = 0;
  93.     }
  94.  
  95.     queue<node*> q;
  96.     root->link = root;
  97.     for (auto p : root->to) {
  98.         p.second->link = root;
  99.         q.push(p.second);
  100.     }
  101.  
  102.     while (!q.empty()) {
  103.         node* start = q.front();
  104.         q.pop();
  105.         for (auto p : start->to) {
  106.             char pch = p.first;
  107.             node* child = p.second;
  108.             node* psuf = start->link;
  109.             while (psuf != root && !psuf->to[pch]) {
  110.                 psuf = psuf->link;
  111.             }
  112.             child->link = (psuf->to[pch] ? psuf->to[pch] : root);
  113.             q.push(child);
  114.         }
  115.     }
  116.  
  117.     node* v = root;
  118.     for (char c : t) {
  119.         node* u = v;
  120.         u = go(v, c, root);
  121.         while (u != root) {
  122.             if (u->terminal) {
  123.                 b[a[u]] = 1;
  124.             }
  125.             u = u->link;
  126.         }
  127.         v = go(v, c, root);
  128.     }
  129.  
  130.     for (string s : vec) {
  131.         cout << (b[s] ? "Yes\n" : "No\n");
  132.     }
  133.  
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement