ICPC 2019, World Finals G. First of Her Name [广义后缀自动机]

后缀自动机的 \(fa_i\)\(i\) 的后缀,所以直接跑就好了啊233

// powered by c++11
// by Isaunoya
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
#define Tp template
using pii = pair<int, int>;
#define fir first
#define sec second
Tp<class T> void cmax(T& x, const T& y) {
  if (x < y) x = y;
}
Tp<class T> void cmin(T& x, const T& y) {
  if (x > y) x = y;
}
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
Tp<class T> void sort(vector<T>& v) { sort(all(v)); }
Tp<class T> void reverse(vector<T>& v) { reverse(all(v)); }
Tp<class T> void unique(vector<T>& v) { sort(all(v)), v.erase(unique(all(v)), v.end()); }
const int SZ = 1 << 23 | 233;
struct FILEIN {
  char qwq[SZ], *S = qwq, *T = qwq, ch;
#ifdef __WIN64
#define GETC getchar
#else
  char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
#endif
  FILEIN& operator>>(char& c) {
    while (isspace(c = GETC()))
      ;
    return *this;
  }
  FILEIN& operator>>(string& s) {
    while (isspace(ch = GETC()))
      ;
    s = ch;
    while (!isspace(ch = GETC())) s += ch;
    return *this;
  }
  Tp<class T> void read(T& x) {
    bool sign = 0;
    while ((ch = GETC()) < 48) sign ^= (ch == 45);
    x = (ch ^ 48);
    while ((ch = GETC()) > 47) x = (x << 1) + (x << 3) + (ch ^ 48);
    x = sign ? -x : x;
  }
  FILEIN& operator>>(int& x) { return read(x), *this; }
  FILEIN& operator>>(ll& x) { return read(x), *this; }
} in;
struct FILEOUT {
  const static int LIMIT = 1 << 22;
  char quq[SZ], ST[233];
  int sz, O;
  ~FILEOUT() { flush(); }
  void flush() {
    fwrite(quq, 1, O, stdout);
    fflush(stdout);
    O = 0;
  }
  FILEOUT& operator<<(char c) { return quq[O++] = c, *this; }
  FILEOUT& operator<<(string str) {
    if (O > LIMIT) flush();
    for (char c : str) quq[O++] = c;
    return *this;
  }
  Tp<class T> void write(T x) {
    if (O > LIMIT) flush();
    if (x < 0) {
      quq[O++] = 45;
      x = -x;
    }
    do {
      ST[++sz] = x % 10 ^ 48;
      x /= 10;
    } while (x);
    while (sz) quq[O++] = ST[sz--];
  }
  FILEOUT& operator<<(int x) { return write(x), *this; }
  FILEOUT& operator<<(ll x) { return write(x), *this; }
} out;

int n, m;
const int maxn = 2e6 + 62;
char s[maxn];

long long ans = 0;

struct SAM {
  int ch[maxn][26], cnt, len[maxn], fa[maxn], sz[maxn];

  SAM() { cnt = 1; }

  int ins(int c, int las) {
    int p = las, np = ++cnt;
    len[np] = len[p] + 1, sz[np] = 1;
    for (; p && !ch[p][c]; p = fa[p]) ch[p][c] = np;
    if (!p) {
      fa[np] = 1;
    } else {
      int q = ch[p][c];
      if (len[q] == len[p] + 1) {
        fa[np] = q;
      } else {
        int nq = ++cnt;
        memcpy(ch[nq], ch[q], sizeof(ch[q]));
        fa[nq] = fa[q], fa[q] = fa[np] = nq, len[nq] = len[p] + 1;
        for (; p && ch[p][c] == q; p = fa[p]) ch[p][c] = nq;
      }
    }
    return np;
  }

  int buc[maxn], sa[maxn];
  void calc() {
    rep(i, 1, cnt) buc[len[i]]++;
    rep(i, 1, cnt) buc[i] += buc[i - 1];
    rep(i, 1, cnt) sa[buc[len[i]]--] = i;
    for (int i = cnt; i; i--) {
      int p = sa[i];
      sz[fa[p]] += sz[p];
    }
  }

  int qry(string s) {
    int p = 1;
    for (char x : s) {
      int c = x - 'A';
      if (!ch[p][c]) {
        return 0;
      } else {
        p = ch[p][c];
      }
    }
    return sz[p];
  }
} sam;

struct Trie {
  int ch[maxn][26], cnt, qwq[maxn], fa[maxn];

  Trie() { cnt = 1; }

  int pos[maxn];

  void buildsam() {
    queue<int> q;
    for (int i = 0; i < 26; i++)
      if (ch[1][i]) q.push(ch[1][i]);
    pos[1] = 1;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      pos[u] = sam.ins(qwq[u], pos[fa[u]]);
      for (int i = 0; i < 26; i++)
        if (ch[u][i]) q.push(ch[u][i]);
    }
  }
} trie;

int main() {
  in >> n >> m;
  n++;
  rep(i, 2, n) {
    char c;
    int _;
    in >> c >> _, ++_;
    trie.fa[i] = _, trie.qwq[i] = c - 'A', trie.ch[_][c - 'A'] = i;
  }
  trie.buildsam();
  sam.calc();
  while (m--) {
    string s;
    in >> s, reverse(s.begin(), s.end());
    out << sam.qry(s) << '\n';
  }
  return 0;
}
posted @ 2020-03-15 13:39  _Isaunoya  阅读(159)  评论(0编辑  收藏  举报