2022HDU-Multi-University Training AC/DC
题目链接:https://acm.hdu.edu.cn/showproblem.php?pid=7192
LCT维护一下fail树形态,询问时把删除部分和没有被删除的部分交线附近暴力匹配一下。
#include<bits/stdc++.h>
#define N 400009
using namespace std;
typedef long long ll;
char s[N],s1[N];
int n,q;
int ch[N][26],siz[N],nxt[N];
int tr[N][2],si[N],val[N],f[N],fa[N];
int last,cnt,l[N],num,tg[N];
inline void pushup(int x){siz[x]=siz[tr[x][0]]+siz[tr[x][1]]+si[x]+val[x];}
inline bool ge(int x){return tr[f[x]][1]==x;}
inline bool isroot(int x){return tr[f[x]][0]!=x&&tr[f[x]][1]!=x;}
inline void rotate(int x){
int y=f[x],o=ge(x);
if(isroot(x))return;
tr[y][o]=tr[x][o^1];f[tr[y][o]]=y;
if(!isroot(y))tr[f[y]][ge(y)]=x;f[x]=f[y];
f[y]=x;tr[x][o^1]=y;pushup(y);pushup(x);
}
inline void splay(int x){
while(!isroot(x)){
int y=f[x];
if(isroot(y))rotate(x);
else rotate(ge(x)==ge(y)?y:x),rotate(x);
}
}
inline void access(int x){
for(int y=0;x;y=x,x=f[x]){
splay(x);
si[x]+=siz[tr[x][1]];si[x]-=siz[y];tr[x][1]=y;
pushup(x);
}
}
inline void link(int x,int y){
access(x);splay(x);access(y);splay(y);
f[x]=y;si[y]+=siz[x];pushup(y);
}
inline void cut(int x,int y){
access(x);splay(x);
f[tr[x][0]]=0;tr[x][0]=0;
pushup(x);
}
inline void del(int x){
access(x);splay(x);
val[x]=0;
pushup(x);
}
inline void ins(int x){
int p=last,np=++cnt;last=np;l[np]=l[p]+1;
siz[np]=val[np]=1;
for(;p&&!ch[p][x];p=fa[p])ch[p][x]=np;
if(!p)fa[np]=1,link(np,1);
else{
int q=ch[p][x];
if(l[p]+1==l[q])fa[np]=q,link(np,q);
else{
int nq=++cnt;l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
int y=fa[q];cut(q,y);link(nq,y);
fa[nq]=fa[q];fa[q]=fa[np]=nq;
link(q,nq);link(np,nq);
for(;ch[p][x]==q;p=fa[p])ch[p][x]=nq;
}
}
}
int main(){
ios::sync_with_stdio(false);
int T;
cin>>T;
int x;
while(T--){
cin>>(s+1)>>q;
n=strlen(s+1);
last=cnt=1;
for(int i=1;i<=n;++i){
ins(s[i]-'a');
tg[i]=last;
}
int opt;
int lst=0;
while(q--){
cin>>opt;
if(opt==1){
cin>>s1;
s1[0]=((s1[0]-'a')^lst)%26+'a';
// cout<<s1<<"wowow"<<endl;
++n;
s[n]=s1[0];
ins(s1[0]-'a');
tg[n]=last;
}
if(opt==2){
num++;
del(tg[num]);
}
if(opt==3){
cin>>(s1+1);
int len=strlen(s1+1);
int now=1;
for(int i=1;i<=len;++i){
s1[i]=((s1[i]-'a')^lst)%26+'a';
now=ch[now][s1[i]-'a'];
}
// cout<<s1+1<<" ?? "<<now<<endl;
if(!now){
puts("0");
lst=0;
}
else{
access(now);splay(now);
int ans=siz[now]-siz[tr[now][0]];
int l=max(1,num-len+2);
int r=num+len-1;
int j=0;
for(int i=2;i<=len;++i){
if(j&&s1[j+1]!=s1[i])j=nxt[j];
if(s1[j+1]==s1[i])j++;
nxt[i]=j;
}
j=0;
for(int i=l;i<=r;++i){
if(j&&s1[j+1]!=s[i])j=nxt[j];
if(s1[j+1]==s[i])j++;
if(j==len)ans--,j=nxt[j];
}
printf("%d\n",lst=ans);
}
}
}
for(int i=0;i<=cnt;++i){
memset(ch[i],0,sizeof(ch[i]));
siz[i]=nxt[i]=tr[i][0]=tr[i][1]=si[i]=val[i]=f[i]=fa[i]=l[i]=0;
}
num=0;
}
return 0;
}