P5840 [COCI2015]Divljak [AC自动机,链并]

// 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 int long long
using pii = pair<int, int>;
#define ve vector
#define Tp template
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
#define fir first
#define sec second
// the cmin && cmax
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;
}
// sort , unique , reverse
Tp<class T> void sort(ve<T>& v) { sort(all(v)); }
Tp<class T> void unique(ve<T>& v) {
  sort(all(v));
  v.erase(unique(all(v)), v.end());
}
Tp<class T> void reverse(ve<T>& v) { reverse(all(v)); }
const int SZ = 0x191981;
struct FILEIN {
  ~FILEIN() {}
  char qwq[SZ], *S = qwq, *T = qwq, ch;
  char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
  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 = 1;
    while ((ch = GETC()) < 0x30)
      if (ch == 0x2d) sign = 0;
    x = (ch ^ 0x30);
    while ((ch = GETC()) > 0x2f) x = x * 0xa + (ch ^ 0x30);
    x = sign ? x : -x;
  }
  FILEIN& operator>>(int& x) { return read(x), *this; }
  FILEIN& operator>>(signed& x) { return read(x), *this; }
  FILEIN& operator>>(unsigned& x) { return read(x), *this; }
} in;
struct FILEOUT {
  const static int LIMIT = 0x114514;
  char quq[SZ], ST[0x114];
  signed 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++] = 0x2d;
      x = -x;
    }
    do {
      ST[++sz] = x % 0xa ^ 0x30;
      x /= 0xa;
    } while (x);
    while (sz) quq[O++] = ST[sz--];
    return;
  }
  FILEOUT& operator<<(int x) { return write(x), *this; }
  FILEOUT& operator<<(signed x) { return write(x), *this; }
  FILEOUT& operator<<(unsigned x) { return write(x), *this; }
} out;

int n, q;
const int maxn = 2e6 + 62;
int ch[maxn][26], cnt = 1, ed[maxn], fail[maxn];

int ins(string s) {
  int p = 1;
  for (char c : s) {
    int x = c - 'a';
    if (!ch[p][x]) ch[p][x] = ++cnt;
    p = ch[p][x];
  }
  return p;
}

void buildfail() {
  queue<int> q;
  for (int i = 0; i < 26; i++)
    if (ch[1][i])
      fail[ch[1][i]] = 1, q.push(ch[1][i]);
    else
      ch[1][i] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < 26; i++)
      if (ch[u][i])
        fail[ch[u][i]] = ch[fail[u]][i], q.push(ch[u][i]);
      else
        ch[u][i] = ch[fail[u]][i];
  }
}

vector<int> g[maxn];
int sz[maxn], d[maxn], son[maxn];
void dfs(int u) {
  sz[u] = 1;
  for (int v : g[u]) {
    d[v] = d[u] + 1;
    dfs(v), sz[u] += sz[v];
    if (sz[v] > sz[son[u]]) son[u] = v;
  }
}
int dfn[maxn], idx = 0, top[maxn];
void dfs(int u, int t) {
  top[u] = t, dfn[u] = ++idx;
  if (son[u]) dfs(son[u], t);
  for (int v : g[u])
    if (v ^ son[u]) dfs(v , v);
}

int lca(int u, int v) {
  while (top[u] ^ top[v]) {
    if (d[top[u]] < d[top[v]]) u ^= v ^= u ^= v;
    u = fail[top[u]];
    // fa_u = fail_u
  }
  return d[u] < d[v] ? u : v;
}

// bit
int c[maxn];
int low(int x) { return x & -x; }
void upd(int x, int y) {
  for (; x <= cnt; x += low(x)) c[x] += y;
}
int qry(int x) {
  int ans = 0;
  for (; x; x ^= low(x)) ans += c[x];
  return ans;
}

signed main() {
#ifdef _WIN64
  freopen("testdata.in", "r", stdin);
#else
  ios_base ::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
#endif
  // code begin.
  in >> n;
  rep(i, 1, n) {
    string s;
    ed[i] = ins((in >> s, s));
  }
  buildfail();
  rep(i, 2, cnt) g[fail[i]].pb(i);
  dfs(1), dfs(1, 1);
  in >> q;
  while (q--) {
    int op;
    in >> op;
    if (op == 1) {
      string s;
      in >> s;
      int p = 1;
      vector<int> P;
      for (char c : s) {
        int x = c - 'a';
        p = ch[p][x], P.pb(p);
      }
      sort(P.begin() , P.end() , [](int x, int y) { return dfn[x] < dfn[y]; });
			P.erase(unique(P.begin() , P.end()) , P.end()) ;
      for (int i = 0; i < sz(P); i++) {
        upd(dfn[P[i]], 1);
        if (i) upd(dfn[lca(P[i - 1] , P[i])], -1);
      }
    } else {
      int x;
			out << (in >> x , qry(dfn[ed[x]] + sz[ed[x]] - 1) - qry(dfn[ed[x]] - 1)) << '
' ; 
    }
  }
  return 0;
  // code end.
}
原文地址:https://www.cnblogs.com/Isaunoya/p/12346670.html