bzoj 2555: SubString【后缀自动机+LCT】
一直WA……找了半天错的发现居然是解密那里的mask其实是不能动的……传进去的会变,但是真实的那个不会变……
然后就是后缀自动机,用LCT维护parent树了……注意不能makeroot,因为自动机的根是不会变的(同理也不能翻转区间),其实就是低配LCT(?)
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=3000005;
int q,n,m,fa[N],ch[N][27],dis[N],la,cur=1,con=1,ma,st[N],top;
char o[10],s[N];
struct qwe
{
int c[2],f,s,lz;
}t[N<<1];
bool srt(int x)
{
return t[t[x].f].c[0]!=x&&t[t[x].f].c[1]!=x;
}
void ud(int x,int y)
{
if(x)
t[x].s+=y,t[x].lz+=y;
}
void pd(int x)
{
if(t[x].lz)
{
ud(t[x].c[0],t[x].lz);
ud(t[x].c[1],t[x].lz);
t[x].lz=0;
}
}
void zhuan(int x)
{
int y=t[x].f,z=t[y].f,l=(t[y].c[0]!=x),r=l^1;
if(!srt(y))
t[z].c[t[z].c[0]!=y]=x;
t[x].f=z;
t[t[x].c[r]].f=y,t[y].c[l]=t[x].c[r];
t[x].c[r]=y,t[y].f=x;
}
void splay(int x)
{
top=0;
st[++top]=x;
for(int i=x;!srt(i);i=t[i].f)
st[++top]=t[i].f;
for(int i=top;i>=1;i--)
pd(st[i]);
while(!srt(x))
{
int y=t[x].f,z=t[y].f;
if(!srt(y))
((t[y].c[0]==x)^(t[z].c[0]==y))?zhuan(x):zhuan(y);
zhuan(x);
}
}
void acc(int x)
{
for(int i=0;x;i=x,x=t[x].f)
{
splay(x);
t[x].c[1]=i;
}
}
void lk(int x,int y)
{
t[x].f=y;
acc(y);
splay(y);
ud(y,t[x].s);
}
void ct(int x)
{
acc(x);
splay(x);
ud(t[x].c[0],-t[x].s);
t[t[x].c[0]].f=0,t[x].c[0]=0;
}
void ins(int c)
{
la=cur,dis[cur=++con]=dis[la]+1,t[cur].s=1;
int p=la;
for(;p&&!ch[p][c];p=fa[p])
ch[p][c]=cur;
if(!p)
fa[cur]=1,lk(cur,1);
else
{
int q=ch[p][c];
if(dis[q]==dis[p]+1)
fa[cur]=q,lk(cur,q);
else
{
int nq=++con;
dis[nq]=dis[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q];
lk(nq,fa[q]);
fa[q]=fa[cur]=nq;
ct(q),lk(q,nq),lk(cur,nq);
for(;ch[p][c]==q;p=fa[p])
ch[p][c]=nq;
}
}
}
int ques(int len)
{
int nw=1;
for(int i=0;i<len;i++)
if(!(nw=ch[nw][s[i]-'A']))
return 0;
splay(nw);
return t[nw].s;
}
int main()
{
scanf("%d%s",&q,s+1);
n=strlen(s+1);
for(int i=1;i<=n;i++)
ins(s[i]-'A');
while(q--)
{
scanf("%s%s",o+1,s);
m=strlen(s);
for(int i=0,re=ma;i<m;i++)
{
re=(re*131+i)%m;
swap(s[i],s[re]);
}
if(o[1]=='Q')
{
int ans=ques(m);
printf("%d\n",ans);
ma^=ans;
}
else
{
for(int i=0;i<m;i++)
ins(s[i]-'A');
}
}
return 0;
}
bzoj 2555: SubString【后缀自动机+LCT】的更多相关文章
- bzoj 2555 SubString —— 后缀自动机+LCT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2555 建立后缀自动机,就可以直接加入新串了: 出现次数就是 Right 集合的大小,需要查询 ...
- 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 ...
- BZOJ 2555: SubString 后缀自动机_LCT
很水的一道题,就是有些细节没注意到. 比如说将调试信息误以为是最终结果而多调了20分钟QAQ ..... 我们注意到,每新加一个节点,改变的是该节点沿着 Parent 走一直走到根节点. 对应的,在 ...
- bzoj 2555 SubString(SAM+LCT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2555 [题意] 给定一个字符串,可以随时插入字符串,提供查询s在其中作为连续子串的出现 ...
- 【BZOJ2555】SubString 后缀自动机+LCT
[BZOJ2555]SubString Description 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2 ...
- bzoj 5408: string 后缀自动机 + LCT
联赛前练练码力. code: #include <vector> #include <cstdio> #include <cstring> #include < ...
- ●BZOJ 2555 SubString
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2555题解: 后缀自动机+LCT 不难发现,对于输入的询问串,在自动机里trans后的到的状态 ...
- 字符串(LCT,后缀自动机):BZOJ 2555 SubString
2555: SubString Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1620 Solved: 471 Description 懒得写背景了 ...
随机推荐
- docker save docker load
docker save && docker load docker save 镜像1 镜像2 | gzip > images.tar.gz 打包镜像为压缩文件 docker sa ...
- IOS UIPickView+sqlite 选择中国全部城市案例
1.案例简单介绍 通过读取文件.将中国全部城市写入sqlite数据库中,现通过UIPickView实现中国全部城市的选择,效果图例如以下所看到的 2.城市对象模型 中国全部城市数据请看http://b ...
- weex 小结
1. import 文件时,必须引入全称,不能省略 .vue import mEcharts from '../components/Echarts.vue' 2.weex 的 cli 中没有 配置 ...
- 菜鸟系列之C/C++经典试题(三)
设计包括min函数的栈 题目:定义栈的数据结构,要求加入一个min函数,可以得到栈的最小元素.要求函数min.push以及pop的时间复杂度都是O(1). 分析:这是2006年google的一道面试题 ...
- js对象的属性问题
ES6之前js的对象的属性只能是字符串, <html> <head> <script type="text/javascript"> var a ...
- shell学习五十六天----延迟进程调度
延迟进程调度 前言:大部分时候,我们都希望进程快点開始,开点结束,别卡.而shell的运行,也是在前一个命令后,立即接着运行下一个命令.命令完毕的速度是与资源的限制有关,且不在shell的权限下. 在 ...
- Sublime Text web开发神器
开发工具介绍 开发工具一般分为两种类型:文本编辑器和集成开发环境(IDE) 常用的文本编辑器:Sublime Text.Notepad++.EditPlus等 常用的IDE:WebStorm.Inte ...
- IOS开发,知识点小结,ios开发中经常使用的宏定义总结
IOS开发,从应用跳转到用浏览器打开网页: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http:// ...
- linux下自动创建设备文件节点---class
在驱动模块初始化函数中实现设备节点的自动创建 我们在刚开始写Linux设备驱动程序的时候,很多时候都是利用mknod命令手动创建设备节点,实际上Linux内核为我们提供了一组函数,可以用来在模块加载的 ...
- 通过powershell操作eventlog
relevant command list ~\Desktop> (Get-Command Write-EventLog).Parameters Key Value --- ----- Warn ...