1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| #include <bits/stdc++.h> using namespace std;
namespace Legendgod {
namespace Read {
#ifdef Fread const int Siz = (1 << 21) + 5; char buf[Siz], *iS, *iT; #define gc() (iS == iT ? (iT = (iS = buf) + fread(buf, 1, Siz, stdin), iT == iS ? EOF : *iS ++ ) : *iS ++) #define getchar gc #endif
template <typename T> void r1(T &x) { x = 0; int f(1); char c(getchar()); for(; !isdigit(c); c = getchar()) if(c == '-') f = -1; for(; isdigit(c); c = getchar()) x = (x * 10) + (c ^ 48); x *= f; } template <typename T, typename...Args> void r1(T &x, Args&...arg) { r1(x), r1(arg...); }
}
using namespace Read;
const int maxn = 2e5 + 5; char s[maxn]; struct Node { int ch[26], fl, opt, fa; Node(void) { fl = opt = fa = 0; memset(ch, 0, sizeof(ch)); } }t[maxn];
int id[maxn]; vector<pair<int, int> > vc[maxn];
int ans[maxn];
int tot(1);
struct Graph { int head[maxn], cnt; struct Edge { int to, next; }edg[maxn << 1]; Graph(void) : cnt(0) {} void add(int u,int v) {
edg[++ cnt] = (Edge) {v, head[u]}, head[u] = cnt; } }G; constexpr int N = 26; int ch[maxn][26]; void build() { for(int i = 1; i <= tot; ++ i) for(int j = 0; j < N; ++ j) ch[i][j] = t[i].ch[j]; static queue<int> q; while(!q.empty()) q.pop(); for(int i = 0; i < N; ++ i) if(t[1].ch[i]) q.push(t[1].ch[i]), t[t[1].ch[i]].fl = 1; else t[1].ch[i] = 1; while(!q.empty()) { int u = q.front(); q.pop(); for(int i = 0; i < N; ++ i) if(t[u].ch[i]) { t[t[u].ch[i]].fl = t[t[u].fl].ch[i]; q.push(t[u].ch[i]); } else t[u].ch[i] = t[t[u].fl].ch[i]; } for(int i = 2; i <= tot; ++ i) G.add(t[i].fl, i); } struct Seg { int t[maxn << 1]; int lowbit(int x) { return x & -x; } void add(int p,int c) { for(; p <= tot; p += lowbit(p)) t[p] += c; } int ask(int p) { int res(0); for(; p > 0; p -= lowbit(p)) res += t[p]; return res; } int ask(int l,int r) { return ask(r) - ask(l - 1); } }T; int dfin[maxn], dfot[maxn]; int dfntot; void dfs1(int p,int pre) { dfin[p] = ++ dfntot; for(int i = G.head[p];i;i = G.edg[i].next) { int to = G.edg[i].to; if(to == pre) continue; dfs1(to, p); } dfot[p] = dfntot; } void dfs2(int p,int pre) { T.add(dfin[p], 1);
if(t[p].opt) for(auto v : vc[p]) ans[v.second] += T.ask(dfin[v.first], dfot[v.first]); for(int i = 0;i < N; ++ i) if(ch[p][i]) { dfs2(ch[p][i], p); } T.add(dfin[p], -1); }
int m, n;
signed main() { int i, j; scanf("%s", s + 1); int Ln = strlen(s + 1); int now(1); for(i = 1; i <= Ln; ++ i) { if('a' <= s[i] && s[i] <= 'z') {
if(!t[now].ch[s[i] - 'a']) t[now].ch[s[i] - 'a'] = ++ tot; int tmp = now; now = t[now].ch[s[i] - 'a']; t[now].fa = tmp; } else if(s[i] == 'P') t[now].opt = 1, id[++ n] = now; else now = t[now].fa; } r1(m); for(i = 1; i <= m; ++ i) { int x, y; r1(x, y); vc[id[y]].push_back(make_pair(id[x], i)); } build(); dfs1(1, 0);
dfs2(1, 0); for(i = 1; i <= m; ++ i) printf("%d\n", ans[i]); return 0; }
}
signed main() { return Legendgod::main(), 0; }
|