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
| #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <utility> #include <queue> #include <functional> #define ms(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long ll; typedef pair<int,int> PII; const int mod = 998244353; const int inf = 1 << 30; const int maxn = 500000 + 5; struct BIT { int a[maxn]; void update(int i, int x) { for (; i < maxn; i += i & -i) a[i] += x; } int query(int i) { int r = 0; for (; i; i -= i & -i) r += a[i]; return r; } int query(int l, int r) { return query(r) - query(l - 1); } } g; namespace ACAM { static const int maxp = 500000 + 5; int sz, ch[maxp][26], fail[maxp]; int node() { ms(ch[++sz], 0); fail[sz] = 0; return sz; } void clear() { sz = 0; node(); for (int i = 0; i < 26; i++) ch[0][i] = 1; } int insert(char* s) { int u = 1; for (int i = 0; s[i]; i++) { int v = s[i] - 'a'; if (!ch[u][v]) ch[u][v] = node(); u = ch[u][v]; } return u; } vector<int> edge[maxp]; int tin[maxp], tout[maxp]; void build() { queue<int> q; q.push(1); while (!q.empty()) { int t = q.front(); q.pop(); for (int i = 0; i < 26; i++) { if (ch[t][i]) { fail[ch[t][i]] = ch[fail[t]][i]; q.push(ch[t][i]); } else { ch[t][i] = ch[fail[t]][i]; } } } for (int i = 2; i <= sz; i++) edge[fail[i]].push_back(i); int tot = 0; function<void(int)> dfs = [&](int u) { tin[u] = ++tot; for (int v: edge[u]) { dfs(v); } tout[u] = tot; }; dfs(1); } } using ACAM::ch; using ACAM::tin; using ACAM::tout; int n, q, ans[maxn]; int trie[maxn][26], sz = 1, pos[maxn]; char s[maxn]; vector<PII> que[maxn]; void dfs(int u, int x) { g.update(tin[x], 1); for (auto q: que[u]) { int v = q.first, id = q.second; ans[id] = g.query(tin[v], tout[v]); } for (int c = 0; c < 26; c++) { if (!trie[u][c]) continue; int v = trie[u][c]; dfs(v, ACAM::ch[x][c]); } g.update(tin[x], -1); } int main() { ACAM::clear(); scanf("%d", &n); for (int i = 1, op, fa; i <= n; i++) { scanf("%d", &op); char s[5]; if (op == 1) { scanf("%s", s); fa = 1; } else { scanf("%d%s", &fa, s); fa = pos[fa]; } int c = s[0] - 'a'; if (!trie[fa][c]) trie[fa][c] = ++sz; pos[i] = trie[fa][c]; } scanf("%d", &q); for (int i = 1, t; i <= q; i++) { scanf("%d%s", &t, s); int u = ACAM::insert(s); que[pos[t]].push_back({u, i}); } ACAM::build(); dfs(1, 1); for (int i = 1; i <= q; i++) printf("%d\n", ans[i]); return 0; }
|