BZOJ2555: SubString(后缀自动机,LCT维护Parent树)
Description
Input
Output
Sample Input
A
QUERY B
ADD BBABBBBAAB
Sample Output
解题思路:
代码:
#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树)的更多相关文章
- bzoj 2555 SubString —— 后缀自动机+LCT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2555 建立后缀自动机,就可以直接加入新串了: 出现次数就是 Right 集合的大小,需要查询 ...
- luogu5212/bzoj2555 substring(后缀自动机+动态树)
对字符串构建一个后缀自动机. 每次查询的就是在转移边上得到节点的parent树中后缀节点数量. 由于强制在线,可以用动态树维护后缀自动机parent树的子树和. 注意一个玄学的优化:每次在执行连边操作 ...
- 【BZOJ2555】SubString 后缀自动机+LCT
[BZOJ2555]SubString Description 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2 ...
- [Bzoj3676][Apio2014]回文串(后缀自动机)(parent树)(倍增)
3676: [Apio2014]回文串 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 3396 Solved: 1568[Submit][Statu ...
- bzoj 3881 [Coci2015]Divljak——LCT维护parent树链并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3881 对 S 建 SAM ,每个 T 会让 S 的 parent 树的链并答案+1:在 T ...
- bzoj 2555: SubString 后缀自动机+LCT
2555: SubString Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 688 Solved: 235[Submit][Status][Dis ...
- bzoj 2555 SubString——后缀自动机+LCT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2555 要维护 right 集合的大小.因为 fa 会变,且 fa 构成一棵树,所以考虑用 L ...
- BZOJ2555 SubString 【后缀自动机 + LCT】
题目 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支持这些操作. 输入 ...
- BZOJ2555 SubString(后缀自动机+LCT)
询问串放在SAM上不跳fail跑到的节点的|right|即为答案.用LCT维护parent树即可.可以直接维护子树信息,也可以转化为路径加.注意强制在线所使用的mask是作为参数传进去的. #incl ...
随机推荐
- hdu 3068 最长回文 【Manacher求最长回文子串,模板题】
欢迎关注__Xiong的博客: http://blog.csdn.net/acmore_xiong?viewmode=list 最长回文 ...
- CAShapeLayer的简单介绍以及基本使用
1.CAShapeLayer简单介绍 1.1CAShapeLayer继承于CALayer,能够使用CALayer的全部属性值: 1.2CAShapeLayer须要贝塞尔曲线配合使用才有意义(也 ...
- 123.static静态函数和函数模板
#include <iostream> using namespace std; //static成员,每个类型都会实例化,创建一个变量,类型一致则共享,否则不共享 template &l ...
- Coderfroces 862 C. Mahmoud and Ehab and the xor
C. Mahmoud and Ehab and the xor Mahmoud and Ehab are on the third stage of their adventures now. As ...
- python 多线程学习小记
python对于thread的管理中有两个函数:join和setDaemon setDaemon:如果在程序中将子线程设置为守护线程,则该子线程会在主线程结束时自动退出,设置方式为thread.set ...
- 使用UltraEdit配置多行注释和取消多行注释
UltraEdit功能强大,使用方便,成为软件开发者必备的文档和代码编辑工具.有很多人也直接用它来写代码,如C/Java,脚本如:Perl/Tcl/JavaScript 等. 如果用来写代码,有一个不 ...
- Python3爬虫之爬取某一路径的所有html文件
要离线下载易百教程网站中的所有关于Python的教程,需要将Python教程的首页作为种子url:http://www.yiibai.com/python/,然后按照广度优先(广度优先,使用队列:深度 ...
- 洛谷 P1889 士兵站队
P1889 士兵站队 题目描述 在一个划分成网格的操场上, n个士兵散乱地站在网格点上.由整数 坐标 (x,y) 表示.士兵们可以沿网格边上.下左右移动一步,但在同时刻任一网格点上只能有名士兵.按照军 ...
- 实验记录三 通用输入输出(GPIO)
之前把全部程序都跑了一次后,得到了导师下一步的安排. 例如以下: 1.编写一个程序.实如今LCD上显示一个万年历,包含年月日 星期 还有室内的温度.2.编写一个程序,将原来的交通灯改为跑马灯. 期限是 ...
- UML图和C#
这段时间学习了楚光明老师解说的C#视频,接触这个学习材料的第一感觉就是老师解说的通俗易懂,非常easy让人去接受:再有就是在学习到UML图和C#的一节时非常有收获,之前自己也学习过UML图的一 ...