最蛋疼的是某节点相同字母的后续可能有多个,因此要将原树和字典树配合使用
rmq 算法
#include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <vector> using namespace std; #pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long ll; const int MAXN = 200000; const int MOD = 1000000007; int n; ll f[25], mPow[MAXN]; struct node_edge { int to, next, val; }edge[MAXN<<1]; int head[MAXN], nmE; void addEdge(int fr, int to, int ch) { edge[nmE].to = to; edge[nmE].next = head[fr]; edge[nmE].val = ch; head[fr] = nmE++; edge[nmE].to = fr; edge[nmE].next = head[to]; edge[nmE].val = ch; head[to] = nmE++; } /// tire int c[MAXN][26]; ll val[MAXN]; int ntot; /// end of tire int newNode() { const int sz = 26*4; memset(c[ntot], 0, sz); val[ntot] = 0; return ntot++; } struct Point { int id, tim; bool operator < (const Point & a) const { return tim < a.tim; } }; int step, dis[MAXN]/**记录每个节点想对于1节点的深度*/, tim[MAXN]/**对原树遍历的步骤,在区间上与tire遍历相同*/; int pos[MAXN]/**原节点->tire中节点的映射关系*/; vector<Point> vec[MAXN]/**第i层的的节点*/; int lon[MAXN]/**经过i节点所能构建的字符串最大长度*/, cnt[MAXN]/**从i节点往下的节点数量*/; void dfs(int u, int fa, int cur) { int i; Point tp; for (i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if (v == fa) continue; dis[v] = dis[u] + 1; tim[v] = ++step; tp.id = v, tp.tim = step; vec[dis[v]].push_back(tp); int k = edge[i].val; /** 构建tire树 */ if (!c[cur][k]) { c[cur][k] = newNode(); val[c[cur][k]] = ((ll) val[cur]*26 % MOD + k)%MOD; } pos[v] = c[cur][k]; dfs(v, u, c[cur][k]); lon[u] = max(lon[v], lon[u]); cnt[u] += cnt[v]; } lon[u] = max(lon[u], dis[u]); ++cnt[u]; } int rk[MAXN], g[MAXN]; void cal(int cur) { rk[cur] = step++; g[rk[cur]] = cur; for (int i = 0; i< 26; ++i) { if (c[cur][i]) cal(c[cur][i]); } } void init() { for ( int i = 0; i<= n; ++i) { head[i] = -1; vec[i].clear(); lon[i] = dis[i] = rk[i] = cnt[i] = 0; } ntot = nmE = 0; newNode(); } int T, line[MAXN], dp[25][MAXN]; /// rmq算法,寻找区间的最大值 /** dp[i][j] = max(区间[j, j+2^i-1]) dp[i][j] = max(dp[i-1][j], dp[i-1][j+2^(i-1)]) */ void rmq() { int i, j; T = 0; for ( i = 1; i<= n; ++i) { int l = vec[i].size(); if (l == 0) break; line[i] = T + 1; for (j = 0; j< l; ++j) dp[0][++T] = rk[pos[vec[i][j].id]]; } for (i = 1; f[i] <= T; ++i) { for (j = 1; j+f[i]-1 <= T; ++j) { dp[i][j] = max(dp[i-1][j], dp[i-1][j+f[i-1]]); } } } int m_query(int l, int r) { if (l== r) return dp[0][l]; if (l > r) swap(l, r); int i, k; for (i = 0; i<= 22 ; ++i) { if (f[i] >= (r-l+1)) { k = i-1; break; } } return max(dp[k][l], dp[k][r-f[k]+1]); } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif int i, j, k, cs, q; f[0] = mPow[0] = 1; for (i = 1; i<= 22; ++i) f[i] = (ll) f[i-1]*2%MOD; for (i = 1; i< 100005; ++i) mPow[i] = (ll) mPow[i-1]*26%MOD; char str[5]; scanf("%d", &cs); while (cs--) { scanf("%d", &n); init(); for (i = 1; i< n; ++i) { scanf("%d%d%s", &j, &k, str); addEdge(j, k, str[0]-'a'); } step = 0; dfs(1, -1, 0); step = 0; cal(0); rmq(); pos[1] = 0; scanf("%d", &q); while (q--) { int u, m, dep; scanf("%d%d", &u, &m); if (m == 0) { puts("0"); continue; } if (( dep = dis[u]+m) > lon[u]) { puts("IMPOSSIBLE"); continue; } Point tpm; tpm.tim = tim[u]; int l = lower_bound(vec[dep].begin(), vec[dep].end(), tpm) - vec[dep].begin(); if (l == vec[dep].size()) --l; l += line[dep]; tpm.tim = tim[u] + cnt[u] - 1; int r = upper_bound(vec[dep].begin(), vec[dep].end(), tpm) - vec[dep].begin(); --r; r += line[dep]; int k = m_query(l, r); k = g[k]; u = pos[u]; printf("%I64d\n", (val[k]-val[u]*mPow[m]%MOD+MOD) % MOD); } } return 0; }