LCT无脑维护SAM的right集大小。
别问我为啥范围开到那么大= =
#include <bits/stdc++.h>
#define rep(i,a,b) for (int i = a , _ = b ; i <= _ ; i ++)
#define per(i,a,b) for (int i = a , _ = b ; i >= _ ; i --)
inline int rd() {
char c = getchar();
while (!isdigit(c)) c = getchar() ; int x = c - '0';
while (isdigit(c = getchar())) x = x * 10 + c - '0';
return x;
}
const int maxn = 3000007;
const int maxm = 3000007;
typedef int arr[maxn];
typedef int sam[maxm];
#define lc ch[u][0]
#define rc ch[u][1]
struct LCT {
arr fa , sum , tg , sta;
int ch[maxn][2] , top;
inline bool isrt(int u) {
return ch[fa[u]][0] != u && ch[fa[u]][1] != u;
}
inline void tag(int u , int v) {
if (u) sum[u] += v , tg[u] += v;
}
inline void ps(int u) {
if (u && tg[u]) tag(lc , tg[u]) , tag(rc , tg[u]) , tg[u] = 0;
}
inline void rot(int u) {
int f = fa[u] , g = fa[f] , l , r;
l = (ch[f][1] == u) , r = l ^ 1;
if (!isrt(f)) ch[g][ch[g][1] == f] = u;
fa[u] = g , fa[f] = u;if (ch[u][r]) fa[ch[u][r]] = f;
ch[f][l] = ch[u][r] , ch[u][r] = f;
}
inline void clear(int u) {
for (sta[top ++] = u ; !isrt(u) ; u = fa[u]) sta[top ++] = fa[u];
for (;top;) ps(sta[-- top]);
}
inline void spaly(int u) {
for (clear(u);!isrt(u);rot(u));
}
inline void access(int u) {
int v = u;
for (int t = 0 ; u ; t = u , u = fa[u])
spaly(u) , rc = t;
spaly(v);
}
inline void link(int u , int f) {
fa[u] = f , access(f) , tag(f , sum[u]);
}
inline void cut(int u , int f) {
access(u) , tag(lc , -sum[u]) , fa[lc] = 0 , lc = 0;
}
}lct;
int tot , ed , mask;
sam len , fa;
int go[maxm][26];
int Q , n;
char st[maxn];
inline void add(int c) {
int p = ed , np = ed = ++ tot;
len[np] = len[p] + 1 , lct.sum[np] = 1;
while (p && !go[p][c]) go[p][c] = np , p = fa[p];
if (!p) { fa[np] = 1 ; lct.link(np , 1) ; return; }
int q = go[p][c];
if (len[q] == len[p] + 1) { fa[np] = q ; lct.link(np , q) ; return ; }
int r = ++ tot;
/*lct.sum[r] = lct.sum[q] , */len[r] = len[p] + 1;
memcpy(go[r] , go[q] , sizeof go[q]);
fa[r] = fa[q] , lct.link(r , fa[r]);
lct.cut(q , fa[q]) , fa[q] = fa[np] = r , lct.link(q , r) , lct.link(np , r);
while (p && go[p][c] == q) go[p][c] = r , p = fa[p];
}
void input() {
Q = rd();
scanf("%s" , st + 1);
tot = ed = 1;
for (char *c = st + 1 ; *c ; c ++) add(*c - 'A');
}
inline void get_s(int mask) {
scanf("%s" , st + 1);
n = strlen(st + 1);
rep (i , 0 , n - 1) {
mask = (mask * 131 + i) % n;
std::swap(st[i + 1] , st[mask + 1]);
}
}
inline void add_str() {
get_s(mask);
for (char *c = st + 1 ; *c ; c ++) add(*c - 'A');
}
inline void query() {
get_s(mask);
int u = 1;
rep (i , 1 , n) if (!go[u][st[i] - 'A']) {
puts("0");
return;
} else
u = go[u][st[i] - 'A'];
// lct.access(u);
lct.spaly(u);
mask ^= lct.sum[u];
printf("%d\n" , lct.sum[u]);
}
void solve() {
char cmd[10];
rep (i , 1 , Q) {
scanf("%s" , cmd);
if (cmd[0] == 'A')
add_str();
else
query();
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.txt" , "r" , stdin);
#endif
input();
solve();
return 0;
}