Description

懒得写背景了,给你一个字符串init,要求你支持两个操作
(1):在当前字符串的后面插入一个字符串
(2):询问字符串s在当前字符串中出现了几次?(作为连续子串)
你必须在线支持这些操作。

Input

第一行一个数Q表示操作个数
第二行一个字符串表示初始字符串init
接下来Q行,每行2个字符串Type,Str
Type是ADD的话表示在后面插入字符串。
Type是QUERY的话表示询问某字符串在当前字符串中出现了几次。
为了体现在线操作,你需要维护一个变量mask,初始值为0 

读入串Str之后,使用这个过程将之解码成真正询问的串TrueStr。
询问的时候,对TrueStr询问后输出一行答案Result
然后mask=maskxorResult
插入的时候,将TrueStr插到当前字符串后面即可。
HINT:ADD和QUERY操作的字符串都需要解压
长度 <= 600000,询问次数<= 10000,询问总长度<= 3000000
新加数据一组--2015.05.20

Output

 

Sample Input

2
A
QUERY B
ADD BBABBBBAAB

Sample Output

0

解题思路:

从后缀自动机构造的时候入手,因为Parent树是与pre指针直接相关,就在pre指针构造的时候快速维护Parent(LCT)。
剩下的就是两个模板堆叠,没什么思维含量。

代码:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define lll tr[spc].ch[0]
#define rrr tr[spc].ch[1]
#define ls ch[0]
#define rs ch[1]
class Unzip{
public:
void update(int lastans)
{
mask^=lastans;
return ;
}
void decode(int *a,int len)
{
int upg=mask;
for(int i=;i<=len;i++)
tmp[i-]=a[i];
for(int i=;i<len;i++)
{
upg=(upg*+i)%len;
std::swap(tmp[i],tmp[upg]);
}
for(int i=;i<=len;i++)
a[i]=tmp[i-];
return ;
}
private:
int tmp[];
int mask;
}U;
struct trnt{
int ch[];
int fa;
int is;
int lzt;
int wgt;
int wgti;
bool anc;
}tr[];
struct sant{
int tranc[];
int len;
int pre;
}s[];
int Q;
int siz;
int fin;
int lasans;
char tmp[];
int atp[];
bool whc(int spc)
{
return tr[tr[spc].fa].rs==spc;
}
void pushup(int spc)
{
tr[spc].wgt=tr[spc].wgti+tr[spc].is+tr[lll].wgt+tr[rrr].wgt;
return ;
}
void trr(int spc)
{
if(!spc)
return ;
std::swap(lll,rrr);
tr[spc].lzt^=;
return ;
}
void pushdown(int spc)
{
if(tr[spc].lzt)
{
trr(lll);
trr(rrr);
tr[spc].lzt=;
}
return ;
}
void recal(int spc)
{
if(!tr[spc].anc)
recal(tr[spc].fa);
pushdown(spc);
return ;
}
void rotate(int spc)
{
int f=tr[spc].fa;
bool k=whc(spc);
tr[f].ch[k]=tr[spc].ch[!k];
tr[spc].ch[!k]=f;
if(tr[f].anc)
{
tr[f].anc=;
tr[spc].anc=;
}else
tr[tr[f].fa].ch[whc(f)]=spc;
tr[spc].fa=tr[f].fa;
tr[f].fa=spc;
tr[tr[f].ch[k]].fa=f;
pushup(f);
pushup(spc);
return ;
}
void splay(int spc)
{
recal(spc);
while(!tr[spc].anc)
{
int f=tr[spc].fa;
if(tr[f].anc)
{
rotate(spc);
return ;
}
if(whc(spc)^whc(f))
rotate(spc);
else
rotate(f);
rotate(spc);
}
return ;
}
void access(int spc)
{
int lst=;
while(spc)
{
splay(spc);
tr[spc].wgti-=tr[lst].wgt;
tr[spc].wgti+=tr[rrr].wgt;
tr[rrr].anc=;
tr[lst].anc=;
rrr=lst;
pushup(spc);
lst=spc;
spc=tr[spc].fa;
}
return ;
}
void Mtr(int spc)
{
access(spc);
splay(spc);
trr(spc);
return ;
}
void split(int x,int y)
{
Mtr(x);
access(y);
splay(y);
return ;
}
void link(int x,int y)
{
split(x,y);
tr[x].fa=y;
tr[y].wgti+=tr[x].wgt;
pushup(y);
return ;
}
void cut(int x,int y)
{
split(x,y);
tr[x].fa=;
tr[y].ls=;
tr[x].anc=;
pushup(y);
return ;
}
void Insert(int c)
{
int nwp,nwq,lsp,lsq;
nwp=++siz;
tr[nwp].anc=;
tr[nwp].is=;
s[nwp].len=s[fin].len+;
for(lsp=fin;lsp&&!s[lsp].tranc[c];lsp=s[lsp].pre)
s[lsp].tranc[c]=nwp;
if(!lsp)
{
s[nwp].pre=;
link(nwp,);
}else{
lsq=s[lsp].tranc[c];
if(s[lsq].len==s[lsp].len+)
{
s[nwp].pre=lsq;
link(nwp,lsq);
}else{
nwq=++siz;
tr[nwq].anc=;
s[nwq]=s[lsq];
cut(s[lsq].pre,lsq);
link(s[nwq].pre,nwq);
s[nwq].len=s[lsp].len+;
s[nwp].pre=s[lsq].pre=nwq;
link(nwq,nwp);
link(nwq,lsq);
while(s[lsp].tranc[c]==lsq)
{
s[lsp].tranc[c]=nwq;
lsp=s[lsp].pre;
}
}
}
fin=nwp;
return ;
}
int Query(int root)
{
Mtr();
access(root);
return tr[root].wgti+tr[root].is;
}
int main()
{
fin=++siz;
tr[fin].anc=;
scanf("%d",&Q);
scanf("%s",tmp+);
int len=strlen(tmp+);
for(int i=;i<=len;i++)
Insert(tmp[i]-'A');
while(Q--)
{
scanf("%s",tmp);
if(tmp[]=='A')
{
scanf("%s",tmp+);
len=strlen(tmp+);
for(int i=;i<=len;i++)
atp[i]=tmp[i]-'A';
U.decode(atp,len);
for(int i=;i<=len;i++)
Insert(atp[i]);
}else{
scanf("%s",tmp+);
len=strlen(tmp+);
for(int i=;i<=len;i++)
atp[i]=tmp[i]-'A';
U.decode(atp,len);
int root=;
for(int i=;i<=len;i++)
{
root=s[root].tranc[atp[i]]; }
if(!root)
lasans=;
else
lasans=Query(root);
printf("%d\n",lasans);
U.update(lasans);
}
}
return ;
}

BZOJ2555: SubString(后缀自动机,LCT维护Parent树)的更多相关文章

  1. bzoj 2555 SubString —— 后缀自动机+LCT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2555 建立后缀自动机,就可以直接加入新串了: 出现次数就是 Right 集合的大小,需要查询 ...

  2. luogu5212/bzoj2555 substring(后缀自动机+动态树)

    对字符串构建一个后缀自动机. 每次查询的就是在转移边上得到节点的parent树中后缀节点数量. 由于强制在线,可以用动态树维护后缀自动机parent树的子树和. 注意一个玄学的优化:每次在执行连边操作 ...

  3. 【BZOJ2555】SubString 后缀自动机+LCT

    [BZOJ2555]SubString Description 懒得写背景了,给你一个字符串init,要求你支持两个操作         (1):在当前字符串的后面插入一个字符串         (2 ...

  4. [Bzoj3676][Apio2014]回文串(后缀自动机)(parent树)(倍增)

    3676: [Apio2014]回文串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 3396  Solved: 1568[Submit][Statu ...

  5. bzoj 3881 [Coci2015]Divljak——LCT维护parent树链并

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3881 对 S 建 SAM ,每个 T 会让 S 的 parent 树的链并答案+1:在 T ...

  6. bzoj 2555: SubString 后缀自动机+LCT

    2555: SubString Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 688  Solved: 235[Submit][Status][Dis ...

  7. bzoj 2555 SubString——后缀自动机+LCT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2555 要维护 right 集合的大小.因为 fa 会变,且 fa 构成一棵树,所以考虑用 L ...

  8. BZOJ2555 SubString 【后缀自动机 + LCT】

    题目 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支持这些操作. 输入 ...

  9. BZOJ2555 SubString(后缀自动机+LCT)

    询问串放在SAM上不跳fail跑到的节点的|right|即为答案.用LCT维护parent树即可.可以直接维护子树信息,也可以转化为路径加.注意强制在线所使用的mask是作为参数传进去的. #incl ...

随机推荐

  1. Win8.1应用开发之文件操作

    在操作文件之前,先相应用的应用功能声明进行设定.用户通过C#(非UI)对win8.1上的文件进行訪问,仅仅能局限于图片,音乐,视频和文档四个目录. 而通过文件选取器则能訪问到整个系统的文件. (一)应 ...

  2. generate the call load file

    #!/usr/bin/perl -w $e911_call_percent = 0.0; $ims_node_number = 12; $local_ip = "10.86.52.2&quo ...

  3. BZOJ1901 ZOJ2112 线段树+treap (线段树套线段树)

    BZOJ1901: 线段树套线段树做法: (外层线段树 里层动态开节点的权值线段树) 有一个小小的trick 可以省掉二分变成nlog^2n的 就是把查询的区间都取出来- logn个一起走- 2016 ...

  4. tensorflow学习之路---Session、Variable(变量)和placeholder

    ---恢复内容开始--- 1.Session '''Session.run():首先里面的参数是一个API(函数的接口)的返回值或者是指定参数的值:功能:得知运算结果有两种访问方式:直接建立或者运用w ...

  5. 应用层协议——HTTP

    HTTP: Client HTTP <--> 应用程序HTTP报文 <--> Server HTTP 应用程序 由TCP支持 Statelessprotocol:不会记录客户端 ...

  6. 【Henu ACM Round#14 B】Duff in Love

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 让你在n的因子里面找一个最大的数字x 且x的因子全都不是完全平方数(y^2,y>1) O(sqrt(n))找出n的所有因子. ...

  7. Linux学习总结(6)——CenterOS7安装mysql5.5的方法

    首先centos7 已经不支持mysql,因为收费了你懂得,所以内部集成了mariadb,而安装mysql的话会和mariadb的文件冲突,所以需要先卸载掉mariadb,以下为卸载mariadb,安 ...

  8. android 移植ffmpeg后so库的使用

    今天折腾了一天,可算是有所收获,成功的用jni调用了libffmpeg中的一个方法-----avcodec_version(),至于avcodec_version()是干什么用的我不大清楚,应该是获取 ...

  9. Kinect 开发 —— 骨骼追踪(下)

    Kinect 连线游戏 在纸上将一些列数字(用一个圆点表示)从小到大用线连起来.游戏逻辑很简单,只不过我们在这里要实现的是动动手将这些点连起来,而不是用笔或者鼠标. 在开始写代码之前,需要明确定义我们 ...

  10. Advanced-REST-client-http接口测试工具

    前言 作为一名在IT金字塔底层的苦逼码农也会接触http接口,各位开发大佬肯定也会有需要写或者测试http接口的时候.大多数接口测试工具都需要本地安装客户端,我这里分享一个谷歌浏览器的测试接口插件非常 ...