# [bzoj 2555]Substring

## Description

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

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

## Code

/**************************************************************
Problem: 2555
User: PaperCloud
Language: C++
Result: Accepted
Time:14492 ms
Memory:163144 kb
****************************************************************/

#include<cstring>
#include<cstdio>
#include<algorithm>
#define get(x) (c[fa[x]][1]==x)
#define MS 3000005
#define MX 1200005
char s[MS];
int L,val[MX],lazy[MX];
int fa[MX],c[MX][2];
inline void upd(int x,int v)
{
if(!x)return;
val[x]+=v;
lazy[x]+=v;
}
inline void down(int x)
{
if(!lazy[x])return;
upd(c[x][0],lazy[x]);
upd(c[x][1],lazy[x]);
lazy[x]=0;
}
inline bool nrt(int x)
{
return c[fa[x]][1]==x||c[fa[x]][0]==x;
}
inline void rotate(int x)
{
int y=fa[x],z=fa[y],l=get(x),r=l^1;
if(nrt(y))c[z][get(y)]=x;fa[x]=z;
fa[c[x][r]]=y;c[y][l]=c[x][r];
fa[y]=x;c[x][r]=y;
}
inline void Splay(int x)
{
static int q[MX],top;q[top=1]=x;register int i;
for(i=x;nrt(i);i=fa[i]) q[++top]=fa[i];
for(;top;--top) down(q[top]);
for(;nrt(x);rotate(x))
if(nrt(fa[x])) rotate(get(x)^get(fa[x])?x:fa[x]);
}
inline void access(int x)
{
register int i;
for(i=0;x;x=fa[i=x]) Splay(x),c[x][1]=i;
}
inline void cut(int x)
{
access(x);Splay(x);
upd(c[x][0],-val[x]);
fa[c[x][0]]=0;c[x][0]=0;
}
{
fa[x]=y;
access(y);Splay(y);
upd(y,val[x]);
}
inline void update(int x)
{
if(!x)return;
Splay(x);
}
int fail[MX],ss[MX][26],step[MX];
int last,cnt;
inline void Insert(int x)
{
int p=last,np=++cnt;step[np]=step[p]+1;val[np]=1;
for(;p&&!ss[p][x];p=fail[p]) ss[p][x]=np;
else
{
int q=ss[p][x];
else
{
int nq=++cnt;step[nq]=step[p]+1;
memcpy(ss[nq],ss[q],sizeof ss[q]);
fail[nq]=fail[q];fail[q]=fail[np]=nq;
for(;ss[p][x]==q;p=fail[p]) ss[p][x]=nq;
}
}
last=np;
}
inline int Query(char *s,int L)
{
register int i,x=1;
for(i=0;i<L;++i) x=ss[x][s[i]-'A'];
update(x);return val[x];
}
int main()
{
last=cnt=1;
int i,q,lastans=0;
scanf("%d%s",&q,s+1);L=strlen(s+1);
for(i=1;i<=L;++i) Insert(s[i]-'A');
char opt[10];
while(q--){
scanf("%s%s",opt,s);
L=strlen(s);
for(i=0;i<L;++i) mk=(mk*131+i)%L,std::swap(s[i],s[mk]);
if(opt[0]=='A') for(i=0;i<L;++i) Insert(s[i]-'A');
}
return 0;
}


Blog来自PaperCloud，未经允许，请勿转载，TKS！

posted @ 2019-02-01 14:19  PaperCloud  阅读(98)  评论(0编辑  收藏  举报