洛谷 P5840 / LOJ 3733 「COCI 2015.1」DIVLJAK
思路
对 \(S_1,S_2,...,S_n\) 建出 AC 自动机并建出 \(\mathrm{fail}\) 树。对于每次新加入的 \(P\),考虑计算它对答案的贡献。
考虑在 AC 自动机上匹配的过程。加入一个 \(P\),设它在 AC 自动机上从根结点开始的链经过 \(p_1,p_2,...,p_k\ (k = |P|)\),那么它包含的子串为 \(p_1,p_2,...,p_k\) 到根结点的链经过的所有结点的并。
可以将 \(p_1,p_2,...,p_k\) 先按 dfn 序排序后,对于 \(i \in [1,k]\),将 \(p_i\) 到根结点的链上所有权值 \(+1\);对于 \(i \in [1,k-1]\),将 \(\operatorname{lca}(p_i,p_{i+1})\) 到根结点的链上所有权值 \(-1\)。查询时直接查询对应结束结点的权值即可。这是一个树链加、单点求值的问题,差分后转化为单点加、子树求和,可以树状数组维护。
时间复杂度为 \(O(n \log n + 26n)\)。
代码
code
/*
p_b_p_b txdy
AThousandMoon txdy
AThousandSuns txdy
hxy txdy
*/
#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define scd second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
const int maxn = 2000100;
int n, m, idx[maxn], head[maxn], len, pp[maxn];
int dfn[maxn], times, fa[maxn], sz[maxn], dp[maxn], son[maxn];
int top[maxn], c[maxn];
bool vis[maxn];
char s[maxn];
struct edge {
int to, next;
} edges[maxn << 1];
void add_edge(int u, int v) {
edges[++len].to = v;
edges[len].next = head[u];
head[u] = len;
}
bool cmp(int a, int b) {
return dfn[a] < dfn[b];
}
struct AC {
int ch[maxn][26], tot, fail[maxn];
void insert(char *s, int id) {
int p = 0;
for (int i = 0; s[i]; ++i) {
if (!ch[p][s[i] - 'a']) {
ch[p][s[i] - 'a'] = ++tot;
}
p = ch[p][s[i] - 'a'];
}
idx[id] = p;
}
void build() {
queue<int> q;
for (int i = 0; i < 26; ++i) {
if (ch[0][i]) {
q.push(ch[0][i]);
}
}
while (q.size()) {
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];
}
}
}
for (int i = 1; i <= tot; ++i) {
add_edge(fail[i], i);
}
}
} ac;
int dfs(int u, int f, int d) {
fa[u] = f;
dp[u] = d;
sz[u] = 1;
dfn[u] = ++times;
int maxson = -1;
for (int i = head[u]; i; i = edges[i].next) {
int v = edges[i].to;
if (v == f) {
continue;
}
sz[u] += dfs(v, u, d + 1);
if (sz[v] > maxson) {
son[u] = v;
maxson = sz[v];
}
}
return sz[u];
}
void dfs2(int u, int tp) {
top[u] = tp;
vis[u] = 1;
if (!son[u]) {
return;
}
dfs2(son[u], tp);
for (int i = head[u]; i; i = edges[i].next) {
int v = edges[i].to;
if (!vis[v]) {
dfs2(v, v);
}
}
}
int querylca(int x, int y) {
while (top[x] != top[y]) {
if (dp[top[x]] < dp[top[y]]) {
swap(x, y);
}
x = fa[top[x]];
}
if (dp[x] > dp[y]) {
swap(x, y);
}
return x;
}
inline int lowbit(int x) {
return x & (-x);
}
void update(int x, int d) {
for (int i = x; i <= times; i += lowbit(i)) {
c[i] += d;
}
}
int query(int x) {
int res = 0;
for (int i = x; i; i -= lowbit(i)) {
res += c[i];
}
return res;
}
void solve() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%s", s);
ac.insert(s, i);
}
ac.build();
dfs(0, -1, 1);
dfs2(0, 0);
/*
for (int i = 0; i <= ac.tot; ++i) {
printf("%d ", dfn[i]);
}
putchar('\n');
*/
scanf("%d", &m);
while (m--) {
int op, x;
scanf("%d", &op);
if (op == 1) {
scanf("%s", s + 1);
int p = 0, len = strlen(s + 1);
for (int i = 1; i <= len; ++i) {
p = ac.ch[p][s[i] - 'a'];
pp[i] = p;
}
sort(pp + 1, pp + len + 1, cmp);
for (int i = 1; i <= len; ++i) {
update(dfn[pp[i]], 1);
}
for (int i = 1; i < len; ++i) {
update(dfn[querylca(pp[i], pp[i + 1])], -1);
}
} else {
scanf("%d", &x);
printf("%d\n", query(dfn[idx[x]] + sz[idx[x]] - 1) - query(dfn[idx[x]] - 1));
}
}
}
int main() {
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}