【BZOJ2555】SubString(后缀自动机,Link-Cut Tree)
【BZOJ2555】SubString(后缀自动机,Link-Cut Tree)
题面
题解
这题看起来不难
每次要求的就是\(right/endpos\)集合的大小
所以搞一个\(LCT\)维护一下\(SAM\)的\(Parent\)树就好了
但是代码一点都不好写(我还是对着黄学长的调的。。。)
于是乎我也学着魔改了一下\(LCT\)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define MAX 600006
char ch[MAX];
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
int mask,ans;
struct LCT
{
#define lson (t[x].ch[0])
#define rson (t[x].ch[1])
struct Node
{
int ch[2],ff;
int rev,v,ly;
}t[MAX<<1];
int S[MAX<<1],top;
bool isroot(int x){return t[t[x].ff].ch[0]!=x&&t[t[x].ff].ch[1]!=x;}
void rotate(int x)
{
int y=t[x].ff,z=t[y].ff;
int k=t[y].ch[1]==x;
if(!isroot(y))t[z].ch[t[z].ch[1]==y]=x;t[x].ff=z;
t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;
t[x].ch[k^1]=y;t[y].ff=x;
}
void putrev(int x){swap(lson,rson);t[x].rev^=1;}
void putly(int x,int v){t[x].v+=v;t[x].ly+=v;}
void pushdown(int x)
{
if(t[x].rev)
{
if(lson)putrev(lson);
if(rson)putrev(rson);
t[x].rev^=1;
}
if(t[x].ly!=0)
{
if(lson)putly(lson,t[x].ly);
if(rson)putly(rson,t[x].ly);
t[x].ly=0;
}
}
void Splay(int x)
{
S[top=1]=x;
for(int i=x;!isroot(i);i=t[i].ff)S[++top]=t[i].ff;
while(top)pushdown(S[top--]);
while(!isroot(x))
{
int y=t[x].ff,z=t[y].ff;
if(!isroot(y))
(t[z].ch[1]==y)^(t[y].ch[1]==x)?rotate(x):rotate(y);
rotate(x);
}
}
void access(int x){for(int y=0;x;y=x,x=t[x].ff)Splay(x),rson=y;}
void makeroot(int x){access(x);Splay(x);putrev(x);}
void split(int x,int y){makeroot(x);access(y);Splay(y);}
void cut(int x){access(x);Splay(x);putly(lson,-t[x].v);t[lson].ff=0;lson=0;}
void link(int x,int f){t[x].ff=f;access(f);Splay(f);putly(f,t[x].v);}
int findroot(int x){access(x);Splay(x);while(lson)x=lson;return x;}
}LCT;
struct SAM
{
struct Node
{
int son[26];
int ff,len;
}t[MAX<<1];
int last,tot;
void init(){last=tot=1;}
void extend(int c)
{
int p=last,np=++tot;last=np;LCT.t[np].v=1;
while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff;
if(!p)t[np].ff=1,LCT.link(np,1);
else
{
int q=t[p].son[c];
if(t[q].len==t[p].len+1)t[np].ff=q,LCT.link(np,q);
else
{
int nq=++tot;
t[nq]=t[q];
t[nq].len=t[p].len+1;
LCT.link(nq,t[q].ff);
t[q].ff=t[np].ff=nq;
LCT.cut(q);LCT.link(q,nq);LCT.link(np,nq);
while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff;
}
}
}
}SAM;
void Decode(char *ch,int mask)
{
int l=strlen(ch);
for(int i=0;i<l;++i)
{
mask=(mask*131+i)%l;
swap(ch[i],ch[mask]);
}
}
int main()
{
int Q=read();
SAM.init();
scanf("%s",ch+1);
for(int i=1,l=strlen(ch+1);i<=l;++i)SAM.extend(ch[i]-'A');
while(Q--)
{
scanf("%s",ch);
if(ch[0]=='A')
{
scanf("%s",ch+1);
Decode(ch+1,mask);
for(int i=1,l=strlen(ch+1);i<=l;++i)SAM.extend(ch[i]-'A');
}
else
{
scanf("%s",ch+1);
Decode(ch+1,mask);
int now=1;
for(int i=1,l=strlen(ch+1);i<=l;++i)
now=SAM.t[now].son[ch[i]-'A'];
if(!now)printf("%d\n",ans=0);
else
{
LCT.Splay(now);
printf("%d\n",ans=LCT.t[now].v);
}
mask^=ans;
}
}
return 0;
}
【BZOJ2555】SubString(后缀自动机,Link-Cut Tree)的更多相关文章
- BZOJ2555 SubString【SAM + Link Cut Tree】
BZOJ2555. SubString 要求在线询问一个串在原串中出现的次数,并且可以在原串末尾添加字符串 如果没有修改的话,考虑建出\(parent\)树之后统计每个\(endpos\)节点的\(r ...
- luogu5212/bzoj2555 substring(后缀自动机+动态树)
对字符串构建一个后缀自动机. 每次查询的就是在转移边上得到节点的parent树中后缀节点数量. 由于强制在线,可以用动态树维护后缀自动机parent树的子树和. 注意一个玄学的优化:每次在执行连边操作 ...
- link cut tree 入门
鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题
A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...
- Link/cut Tree
Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...
- 洛谷P3690 Link Cut Tree (模板)
Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门
link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
随机推荐
- sparksql工程小记
最近做一个oracle项目迁移工作,跟着spark架构师学着做,进行一些方法的总结. 1.首先,创建SparkSession对象(老版本为sparkContext) val session = Spa ...
- My GitHub
0.引言 利用python开发,借助Dlib库捕获摄像头中的人脸,进行实时特征点标定: 图1 工程效果示例(gif) 图2 工程效果示例(静态图片) (实现比较简单,代码量也比较少,适合入门或者兴趣学 ...
- tomcat在centos下的操作
一 .Tomcat启动与停止 进入Tomcat中bin所在目录 cd /usr/tomcat/bin 启动: ./startup.sh 停止: ./shutdown.sh 平常可以用这个命令来查看to ...
- php进阶之路--转载
之前有看过相关的文章,觉得还是这篇详细点,有具体的目标实现起来才更有动力 转载自:http://wen.52fhy.com/2016/2016-09-03-PHP-cheng-xu-yuan-xue- ...
- Java语言的特性
一.跨平台 借助虚拟机,程序不经修改即可在不同硬件或者软件平台上运行.源代码级(C,C++源码会重新编译),目标代码级(Java). 二.面向对象 以对象为基本单位,使得程序开发变得简单易用,拓展更方 ...
- HDU - 1407 打表
思路:预处理10000以内所有数的三平方和即可. AC代码 #include <cstdio> #include <cmath> #include <algorithm& ...
- 在Hadoop2.2基础上安装Spark(伪分布式)
没想到,在我的hadoop2.2.0小集群上上安装传说中的Spark竟然如此顺利,可能是因为和搭建Hadoop时比较像,更多需要学习的地方还是scala编程和RDD机制吧 总之,开个好头 原来的集群: ...
- Android中Activity被系统会收前页面信息保存
1.重写onSaveInstanceState方法 protected void onSaveInstanceState(Bundle outState) { super.onSaveInstance ...
- 使用dlib中的深度残差网络(ResNet)实现实时人脸识别
opencv中提供的基于haar特征级联进行人脸检测的方法效果非常不好,本文使用dlib中提供的人脸检测方法(使用HOG特征或卷积神经网方法),并使用提供的深度残差网络(ResNet)实现实时人脸识别 ...
- linux下面的fd限制
如果不考虑内存大小的限制,在linux下面,fd (即file descriptor)的数量来自2个限制(阈值).其一:是操作系统的限制.这个限制主要是在linux内核中,我们知道,用户程序的fope ...