Advertisement
RaFiN_

aho updated

Sep 23rd, 2020
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ll long long
  6.  
  7. const int mod = 1e9 + 7;
  8.  
  9. const int N = 5e5 + 10;
  10.  
  11. string s,pat[1001];int n,tot = 1,par[N],depth[N],pl[N],sl[N],to[N][130],arr[N],lev[N];
  12. vector<int> tr[N];int nxt[N][130],ans[N],vis[N];
  13.  
  14. void Insert(string temp,int indx) {
  15.     int uu = 1;
  16.     for(int i = 0; i < temp.size(); i++) {
  17.         int id = temp[i];
  18.         if(!to[uu][id]) {
  19.             to[uu][id] = ++tot;
  20.             depth[tot] = depth[uu] + 1;
  21.             par[tot] = uu;
  22.             pl[tot] = id;
  23.         }
  24.         uu = to[uu][id];
  25.     }
  26.     lev[indx] = uu;
  27. }
  28.  
  29. void push_links() {
  30.     queue<int> qu;
  31.     qu.push(1);
  32.     while(!qu.empty()) {
  33.         int vv = qu.front();
  34.         qu.pop();
  35.         if(depth[vv] <= 1) sl[vv] = 1;
  36.         else {
  37.             int uu = sl[par[vv]];
  38.             int l = pl[vv];
  39.             while(uu > 1 && !to[uu][l]) uu = sl[uu];
  40.             if(to[uu][l]) uu = to[uu][l];
  41.             sl[vv] = uu;
  42.         }
  43.         if(vv != 1) tr[sl[vv]].push_back(vv);
  44.         for(int i = 0; i < 130; i++) if(to[vv][i]) qu.push(to[vv][i]);
  45.     }
  46. }
  47.  
  48. int jump(int curr,int id) {
  49.     if(nxt[curr][id]) return nxt[curr][id];
  50.     int uu = curr;
  51.     while(curr > 1 && !to[curr][id]) curr = sl[curr];
  52.     if(to[curr][id]) curr = to[curr][id];
  53.     return nxt[uu][id] = curr;
  54. }
  55. void dfs(int s) {
  56.     if(vis[s]) return ;
  57.     vis[s] = 1;
  58.     for(auto it : tr[s]) {
  59.         dfs(it);
  60.         arr[s] += arr[it];
  61.     }
  62. }
  63. int main() {
  64.  
  65.     ios_base::sync_with_stdio(0); cin.tie(0);
  66.     #ifndef ONLINE_JUDGE
  67.     freopen("input.txt", "r", stdin);
  68.     freopen("output.txt", "w", stdout);
  69.     #endif
  70.     cin >> s;
  71.     cin >> n;
  72.     for(int i = 0; i < n; i++) {
  73.         cin >> pat[i];
  74.         Insert(pat[i],i);
  75.     }
  76.     push_links();
  77.     int curr = 1;
  78.     for(int i = 0; i < s.size(); i++) {
  79.         int id = s[i];
  80.         int x = jump(curr,id);
  81.         curr = x;
  82.         arr[x] ++;
  83.         //cout << x << " " << i << endl;
  84.     }
  85.     for(int i = 1; i <= tot; i++) {
  86.         if(!vis[i]) dfs(i);
  87.     }
  88.     for(int i = 0; i < n; i++) {
  89.         if(arr[lev[i]]) cout << "Y";
  90.         else cout << "N";
  91.         //cout << " " << arr[lev[i]] ;
  92.         cout << "\n";
  93.     }
  94.     return 0;
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement