Manioc

otmei no cu

Sep 12th, 2019
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. // by: Emiso
  2. #include <bits/stdc++.h>
  3. #define MAX 1000100
  4. #define ALP 26
  5.  
  6. using namespace std;
  7. typedef pair<int, char> pii;
  8.  
  9. int C(char c) { return c - 'a'; }
  10.  
  11. struct aho {
  12.     int parent[MAX], suffix[MAX], to[MAX][ALP], super[MAX];
  13.     char from[MAX];
  14.     int id[MAX], cnt, built;
  15.     long long ending[MAX], lazy[MAX];
  16.  
  17.     int new_node(int _parent = 0, char _from = ' ') {
  18.         parent[cnt] = _parent; from[cnt] = _from; id[cnt] = -1;
  19.         suffix[cnt] = super[cnt] = ending[cnt] = lazy[cnt] = 0;
  20.         for(int i = 0; i < ALP; i++) to[cnt][i] = 0;
  21.         return cnt++;
  22.     }
  23.  
  24.     aho() {
  25.         cnt = built = 0;
  26.         new_node();
  27.     }
  28.  
  29.     int add(string &word, int _id = 0) {
  30.         int node = 0;
  31.         for(int i = 0; i < word.size(); i++) {
  32.             int nxt = C(word[i]);
  33.             if(!to[node][nxt]) to[node][nxt] = new_node(node, word[i]);
  34.             node = to[node][nxt];
  35.         }
  36.         ending[node]++;
  37.         if(id[node] == -1) id[node] = _id;
  38.         return id[node];
  39.     }
  40.  
  41.     void build() {
  42.         built = 1;
  43.         queue<int> q;
  44.         for(int i = 0; i < ALP; i++)
  45.             if(to[0][i]) q.push(to[0][i]);
  46.  
  47.         while(!q.empty()) {
  48.             int v = q.front(); q.pop();
  49.             if(parent[v]) suffix[v] = to[suffix[parent[v]]][C(from[v])];
  50.  
  51.             if(id[v] != -1) super[v] = v;
  52.             else super[v] = super[suffix[v]];
  53.  
  54.             ending[v] += ending[suffix[v]];
  55.             for(int i = 0; i < ALP; i++) {
  56.                 if(!to[v][i]) to[v][i] = to[suffix[v]][i];
  57.                 else q.push(to[v][i]);
  58.             }
  59.         }
  60.     }
  61.  
  62.     long long search_text(string &text) {
  63.         if(!built) build();
  64.         int prefix = 0;
  65.         long long count = ending[0];
  66.         for(char c : text) {
  67.             prefix = to[prefix][C(c)];
  68.             count += ending[prefix];
  69.         }
  70.         return count;
  71.     }
  72.  
  73.     vector<pii> text[MAX];
  74.     void dfs(int no, int cu){
  75.         //cout << no << " " << cu << endl;
  76.         for(int i = 0; i < text[no].size(); i++){
  77.             auto u = text[no][i];
  78.             int prefix = to[cu][C(u.second)];
  79.             lazy[prefix]++;
  80.             dfs(u.first, prefix);
  81.         }
  82.     }
  83.     void search_text_linear(int *ans) { /// O(N+M)
  84.         if(!built) build();
  85.         dfs(0, 0);
  86.         push_lazy(ans);
  87.     }
  88.  
  89.     void push_lazy(int *ans) {
  90.         vector<int> deg(cnt, 0);
  91.         for(int i = 1; i < cnt; i++)
  92.             deg[suffix[i]]++;
  93.  
  94.         queue<int> fila;
  95.         for(int i = 1; i < cnt; i++)
  96.             if(deg[i] == 0) fila.push(i);
  97.  
  98.         while(!fila.empty()) {
  99.             int u = fila.front(); fila.pop();
  100.  
  101.             if(id[u] != -1) ans[id[u]] += lazy[u];
  102.             lazy[suffix[u]] += lazy[u];
  103.  
  104.             deg[suffix[u]]--;
  105.             if(suffix[u] && deg[suffix[u]] == 0) fila.push(suffix[u]);
  106.         }
  107.     }
  108. } dict;
  109.  
  110. int ans[MAX];
  111. int main() {
  112.     int n, q; cin >> n >> q;
  113.     for(int i = 1; i <= n; i++){
  114.         int ch; char c; cin >> c >> ch;
  115.         dict.text[ch].emplace_back(i, c);
  116.     }
  117.  
  118.     for(int i = 1; i <= q; i++){
  119.         string s; cin >> s;
  120.         reverse(s.begin(), s.end());
  121.         dict.add(s, i);
  122.     }
  123.    
  124.     dict.build();
  125.     dict.search_text_linear(ans);
  126.     for(int i = 1; i <= q; i++) cout << ans[i] << endl;
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment