【题目链接】
【思路要点】
- 分开处理路径上竖直的字符串和在 L c a Lca Lca 处拐弯的字符串。
- 在 L c a Lca Lca 处拐弯的字符串总数在 O ( ∑ ∣ S ∣ ) O(\sum|S|) O(∑∣S∣) 级别,可以直接 K m p Kmp Kmp 判断。
- 竖直的字符串可以在离线询问后通过 A C AC AC 自动机和树状数组处理。
- 时间复杂度 O ( N L o g N + M L o g N + ∑ ∣ S ∣ ) O(NLogN+MLogN+\sum|S|) O(NLogN+MLogN+∑∣S∣) 。
【代码】
#include<bits/stdc++.h> using namespace std; const int MAXL = 6e5 + 5; const int MAXN = 1e5 + 5; const int MAXLOG = 20; typedef long long ll; typedef long double ld; typedef unsigned long long ull; template <typename T> void chkmax(T &x, T y) {x = max(x, y); } template <typename T> void chkmin(T &x, T y) {x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } struct BinaryIndexTree { int n, a[MAXL]; void init(int x) { n = x; memset(a, 0, sizeof(a)); } void modify(int x, int d) { for (int i = x; i <= n; i += i & -i) a[i] += d; } int query(int x) { int ans = 0; for (int i = x; i >= 1; i -= i & -i) ans += a[i]; return ans; } int query(int l, int r) { int ans = 0; for (int i = r; i >= 1; i -= i & -i) ans += a[i]; for (int i = l - 1; i >= 1; i -= i & -i) ans -= a[i]; return ans; } } BIT; vector <int> edge[MAXL]; int timer, dfn[MAXL], rit[MAXL]; void dfs(int pos) { dfn[pos] = ++timer; for (unsigned i = 0; i < edge[pos].size(); i++) dfs(edge[pos][i]); rit[pos] = timer; } namespace ACAutomaton { const int MAXP = 6e5 + 5; const int MAXC = 26; int root, size; int child[MAXP][MAXC]; int depth[MAXP], fail[MAXP]; void init() {root = size = 0; } int index(char x) { return x - 'a'; if (x == 'A') return 0; if (x == 'C') return 1; if (x == 'G') return 2; return 3; } int insert(char *s) { int len = strlen(s + 1), now = root; for (int i = 1; i <= len; i++) { int tmp = index(s[i]); if (child[now][tmp] == 0) { child[now][tmp] = ++size; depth[size] = depth[now] + 1; } now = child[now][tmp]; } return now; } void build() { static int q[MAXP]; int l = 0, r = -1; for (int i = 0; i < MAXC; i++) if (child[root][i]) { q[++r] = child[root][i]; fail[child[root][i]] = root; } while (l <= r) { int tmp = q[l++]; for (int i = 0; i < MAXC; i++) if (child[tmp][i]) { q[++r] = child[tmp][i]; fail[child[tmp][i]] = child[fail[tmp]][i]; } else child[tmp][i] = child[fail[tmp]][i]; } for (int i = 1; i <= size; i++) edge[fail[i]].push_back(i); dfs(root); } } vector <pair <int, int> > a[MAXN]; int n, m, depth[MAXN], fchar[MAXN], father[MAXN][MAXLOG]; void work(int pos, int fa) { depth[pos] = depth[fa] + 1; father[pos][0] = fa; for (int i = 1; i < MAXLOG; i++) father[pos][i] = father[father[pos][i - 1]][i - 1]; for (unsigned i = 0; i < a[pos].size(); i++) if (a[pos][i].first != fa) { fchar[a[pos][i].first] = a[pos][i].second; work(a[pos][i].first, pos); } } int climb(int x, int goal) { for (int i = MAXLOG - 1; i >= 0; i--) if (depth[father[x][i]] >= goal) x = father[x][i]; return x; } int lca(int x, int y) { if (depth[x] < depth[y]) swap(x, y); for (int i = MAXLOG - 1; i >= 0; i--) if (depth[father[x][i]] >= depth[y]) x = father[x][i]; if (x == y) return x; for (int i = MAXLOG - 1; i >= 0; i--) if (father[x][i] != father[y][i]) { x = father[x][i]; y = father[y][i]; } return father[x][0]; } char s[MAXL], t[MAXL]; int ans[MAXN]; vector <pair <int, int> > q[MAXN]; void getans(int pos, int fa, int apos) { BIT.modify(dfn[apos], 1); for (unsigned i = 0; i < q[pos].size(); i++) if (q[pos][i].first <= 0) ans[-q[pos][i].first] -= BIT.query(dfn[q[pos][i].second], rit[q[pos][i].second]); else ans[q[pos][i].first] += BIT.query(dfn[q[pos][i].second], rit[q[pos][i].second]); for (unsigned i = 0; i < a[pos].size(); i++) if (a[pos][i].first != fa) getans(a[pos][i].first, pos, ACAutomaton :: child[apos][a[pos][i].second]); BIT.modify(dfn[apos], -1); } int kmp(char *s, char *t) { static int nxt[MAXL]; int ls = strlen(s + 1); int lt = strlen(t + 1); for (int i = 2; i <= lt; i++) { int j = nxt[i - 1]; while (j != 0 && t[i] != t[j + 1]) j = nxt[j]; if (t[i] == t[j + 1]) j++; nxt[i] = j; } int pos = 0, ans = 0; for (int i = 1; i <= ls; i++) { while (pos != 0 && s[i] != t[pos + 1]) pos = nxt[pos]; if (s[i] == t[pos + 1]) pos++; if (pos == lt) { ans++; pos = nxt[pos]; } } return ans; } int main() { read(n), read(m); for (int i = 1; i <= n - 1; i++) { int x, y; char c; scanf("\n%d%d %c", &x, &y, &c); a[x].push_back(make_pair(y, c - 'a')); a[y].push_back(make_pair(x, c - 'a')); } work(1, 0); for (int i = 1; i <= m; i++) { int x, y, z, len; scanf("\n%d%d %s", &x, &y, s + 1); z = lca(x, y), len = strlen(s + 1); if (depth[y] - depth[z] >= len) { int pos = ACAutomaton :: insert(s); q[y].push_back(make_pair(i, pos)); q[y = climb(y, depth[z] + len - 1)].push_back(make_pair(-i, pos)); } reverse(s + 1, s + len + 1); if (depth[x] - depth[z] >= len) { int pos = ACAutomaton :: insert(s); q[x].push_back(make_pair(i, pos)); q[x = climb(x, depth[z] + len - 1)].push_back(make_pair(-i, pos)); } reverse(s + 1, s + len + 1); int tlen = 0, rlen = depth[x] + depth[y] - 2 * depth[z]; t[depth[x] + depth[y] - 2 * depth[z] + 1] = 0; while (x != z) { t[++tlen] = fchar[x] + 'a'; x = father[x][0]; } while (y != z) { t[rlen--] = fchar[y] + 'a'; y = father[y][0]; } ans[i] = kmp(t, s); } ACAutomaton :: build(); BIT.init(timer); getans(1, 0, ACAutomaton :: root); for (int i = 1; i <= m; i++) writeln(ans[i]); return 0; }