# Description

(1):在当前字符串的后面插入一个字符串

(2):询问字符串s在当前字符串中出现了几次？(作为连续子串)

# Input

Type是QUERY的话表示询问某字符串在当前字符串中出现了几次。

2

A

QUERY B

0

# Solution

LCT+SAM

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=600000+10,MAXM=3000000+10;
char s[MAXM];
#define lc(x) ch[(x)][0]
#define rc(x) ch[(x)][1]
int sum[MAXN<<1],ch[MAXN<<1][2],fa[MAXN<<1],stack[MAXN<<1],cnt,Isum[MAXN<<1],rev[MAXN<<1];
inline bool nroot(int x)
{
return lc(fa[x])==x||rc(fa[x])==x;
}
inline void reverse(int x)
{
std::swap(lc(x),rc(x));
rev[x]^=1;
}
inline void pushup(int x)
{
sum[x]=sum[lc(x)]+sum[rc(x)]+size[x]+Isum[x];
}
inline void pushdown(int x)
{
if(rev[x])
{
if(lc(x))reverse(lc(x));
if(rc(x))reverse(rc(x));
rev[x]=0;
}
}
inline void rotate(int x)
{
int f=fa[x],p=fa[f],c=(rc(f)==x);
if(nroot(f))ch[p][rc(p)==f]=x;
fa[ch[f][c]=ch[x][c^1]]=f;
fa[ch[x][c^1]=f]=x;
fa[x]=p;
pushup(f);
pushup(x);
}
inline void splay(int x)
{
cnt=0;
stack[++cnt]=x;
for(register int i=x;nroot(i);i=fa[i])stack[++cnt]=fa[i];
while(cnt)pushdown(stack[cnt--]);
for(register int y=fa[x];nroot(x);rotate(x),y=fa[x])
if(nroot(y))rotate((lc(y)==x)==(lc(fa[y])==y)?y:x);
pushup(x);
}
inline void access(int x)
{
for(register int y=0;x;x=fa[y=x])
{
splay(x);
Isum[x]+=sum[rc(x)];
rc(x)=y;
Isum[x]-=sum[rc(x)];
pushup(x);
}
}
inline void makeroot(int x)
{
access(x);splay(x);reverse(x);
}
inline void split(int x,int y)
{
makeroot(x);access(y);splay(y);
}
{
makeroot(x);access(y);splay(y);
fa[x]=y;
Isum[y]+=sum[x];
pushup(y);
}
inline void cut(int x,int y)
{
split(x,y);
fa[x]=lc(y)=0;
pushup(y);
}
};
#undef lc
#undef rc
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void extend(int c)
{
int p=las,np=++tot;
las=np;
len[np]=len[p]+1;size[np]=1;
while(p&&!ch[p][c])ch[p][c]=np,p=fa[p];
else
{
int q=ch[p][c];
else
{
int nq=++tot;
memcpy(ch[nq],ch[q],sizeof(ch[nq]));
len[nq]=len[p]+1;T.cut(q,fa[q]);
while(p&&ch[p][c]==q)ch[p][c]=nq,p=fa[p];
}
}
}
{
}
int main()
{
scanf("%s",s);
for(register int i=0,lt=strlen(s);i<lt;++i)extend(s[i]-'A'+1);
while(q--)
{
char opt[8];scanf("%s",opt);scanf("%s",s);
if(opt[0]=='A')
for(register int i=0,lt=strlen(s);i<lt;++i)extend(s[i]-'A'+1);
if(opt[0]=='Q')
{
int j=1,mark=1;
for(register int i=0,lt=strlen(s),c;i<lt;++i)
{
c=s[i]-'A'+1;
if(ch[j][c])j=ch[j][c];
else {mark=0;break;}
}
if(!mark)puts("0");
else
{
T.makeroot(1);T.access(j);
write(T.Isum[j]+size[j],'\n');