bzoj2555
开始时间:19:40
完成时间:21:00
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2555
题目大意:(1):在当前字符串的后面插入一个字符串
(2):询问字符串s在当前字符串中出现了几次?(作为连续子串)
题解:最近在写后缀自动机,求一个字符串中出现了几次就相当与其right集合大小,直接上parent树,因为后缀自动机构造特性,可能在parent树改变边,于是用lct维护;
代码:
- #include<algorithm>
- #include<iostream>
- #include<cstring>
- #include<cstdio>
- #include<cmath>
- #define maxn 1200005
- #define maxl 3000005
- using namespace std;
- int q,n,tot,root,last,mark,m,ri[maxn],lazy[maxn];
- char st[maxl];
- char ss[];
- struct data{
- int fa[maxn],son[maxn][];
- int isroot(int x) {return (son[fa[x]][]!=x && son[fa[x]][]!=x);}
- int which(int x){return son[fa[x]][]==x;}
- void change(int x,int k){ ri[x]+=k; lazy[x]+=k;}
- void pushdown(int x)
- {
- if (!lazy[x]) return;
- if (son[x][]) change(son[x][],lazy[x]);
- if (son[x][]) change(son[x][],lazy[x]);
- lazy[x]=;
- }
- void relax(int x){if (!isroot(x)) relax(fa[x]); pushdown(x);}
- void turn(int x){
- int y=fa[x],wx=which(x),wy=which(y);
- if (!isroot(y)) son[fa[y]][wy]=x; fa[x]=fa[y];
- son[y][wx]=son[x][-wx]; fa[son[x][-wx]]=y;
- son[x][-wx]=y; fa[y]=x;
- }
- void splay(int x)
- {
- relax(x);
- while (!isroot(x))
- {
- if (isroot(fa[x])) turn(x);
- else if (which(x)==which(fa[x])) turn(fa[x]),turn(x);
- else turn(x),turn(x);
- }
- }
- void access(int x)
- {
- for (int p=; x; x=fa[x]){ splay(x); son[x][]=p; p=x;}
- }
- void link(int x,int y){
- fa[x]=y; access(y); splay(y); change(y,ri[x]);
- }
- void cut(int x)
- {
- access(x); splay(x); change(son[x][],-ri[x]); fa[son[x][]]=; son[x][]=;
- }
- }lct;
- struct date{
- int fa[maxn],son[maxn][],val[maxn];
- void prepare(){root=tot=last=;}
- int newnode(int x){val[++tot]=x; return tot;}
- void extend(int x)
- {
- int p=last,np=newnode(val[p]+);ri[np]=;last=np;
- for (; p&&!son[p][x]; p=fa[p]) son[p][x]=np;
- if (!p) fa[np]=root,lct.link(np,root);
- else
- {
- int q=son[p][x];
- if (val[q]==val[p]+){fa[np]=q; lct.link(np,q);}
- else
- {
- int nq=newnode(val[p]+);ri[nq]=;
- memcpy(son[nq],son[q],sizeof(son[q]));
- fa[nq]=fa[q]; lct.cut(q),lct.link(nq,fa[nq]);
- fa[q]=fa[np]=nq; lct.link(q,nq),lct.link(np,nq);
- for (; p && son[p][x]==q; p=fa[p]) son[p][x]=nq;
- }
- }
- }
- void build()
- {for (int i=; i<=m; i++) extend(st[i]-'A');}
- void query(){
- int x,y;
- bool can=;
- x=root;
- for (int i=;i<=m;i++){
- y=st[i]-'A';
- if (!son[x][y]){
- can=;
- break;
- }else{
- x=son[x][y];
- }
- }
- if (can==||x==root) puts("");
- else{
- lct.splay(x);
- printf("%d\n",ri[x]),mark^=ri[x];
- }
- }
- }SAM;
- void unzip(){
- int temp=mark;
- for (int i=;i<=m;i++){
- temp=(temp*+i-)%m+;
- char t=st[i]; st[i]=st[temp],st[temp]=t,temp--;
- }
- }
- int main()
- {
- scanf("%d\n",&q);mark=;
- scanf("%s",st+); m=strlen(st+);
- SAM.prepare();
- SAM.build();
- for (int i=; i<=q; i++)
- {
- scanf("%s",ss);scanf("%s",st+); m=strlen(st+);unzip();
- if (ss[]=='A') SAM.build();
- else SAM.query();
- }
- }
bzoj2555的更多相关文章
- 【BZOJ2555】SubString(后缀自动机,Link-Cut Tree)
[BZOJ2555]SubString(后缀自动机,Link-Cut Tree) 题面 BZOJ 题解 这题看起来不难 每次要求的就是\(right/endpos\)集合的大小 所以搞一个\(LCT\ ...
- 【bzoj2555】 SubString
http://www.lydsy.com/JudgeOnline/problem.php?id=2555 (题目链接) 题意 给出一个初始串,维护两个操作.在原串后面加入一个字符串:询问某个字符串在原 ...
- 【BZOJ2555】SubString 后缀自动机+LCT
[BZOJ2555]SubString Description 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2 ...
- BZOJ2555 SubString【SAM + Link Cut Tree】
BZOJ2555. SubString 要求在线询问一个串在原串中出现的次数,并且可以在原串末尾添加字符串 如果没有修改的话,考虑建出\(parent\)树之后统计每个\(endpos\)节点的\(r ...
- 【BZOJ-2555】SubString 后缀自动机 + LinkCutTree
2555: SubString Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1936 Solved: 551[Submit][Status][Di ...
- bzoj2555: SubString
SAM+LCT维护parent tree版本 虽然说子树维护那套理论需要ETT 不过parent tree的根是固定的,所以用lct加一些奇怪的乱搞就行了 //随手拖个SAM的板子和LCT的板子,然后 ...
- BZOJ2555——SubString
0.题目很短,就不概括了 给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支持这些操作. ...
- bzoj2555(后缀自动机+LCT)
题目描述 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支持这些操作. 题解 做法很自然,建出后缀自动机,维护每个节点的right ...
- 【BZOJ2555】SubString
算是学会sam了吧…… 原题: 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了 ...
随机推荐
- java OPENCV 连通域, Imgproc.findContours 例子,参数说明
http://stackoverflow.com/questions/29491669/real-time-paper-sheet-detection-using-opencv-in-android/ ...
- Windows应用程序要点
一个完整的Windows应用程序除了WinMain函数外,还包含用于处理用户动作和窗口消息的窗口函数. Windows应用程序具有的一些特性: 消息驱动机制 图形设备接口(GDI) 基于资源的程序设 ...
- Centos下安装jdk详解
环境: 系统: [root@Wulaoer ~]# cat /proc/version Linux version 2.6.32-431.el6.x86_64 (mockbuild@c6b8.bsys ...
- zf-关于通知公告显示问题
1 公告结束日期超过当前时间是不能在通知公告上显示出来的 2 无限制时间的公告也是要在通知公告上显示出来的 于是我在后台实现类增加了如下代码 需要注意的是 当初解决第一个问题的时候增加了一个AND 当 ...
- List转换成DataSet实现代码
public DataSet ConvertToDataSet<T>(IList<T> list) { if (list == null || list.Count <= ...
- 1209:Catch That Cow(bfs)
题意: 从一个坐标到另一个坐标的移动方式有三种,即:st-1,st+1,2*st.每移动一步时间是一秒. 给出两个坐标,求得从第一坐标到第二座标的最短时间. #include<iostream& ...
- 通过ant调用shell脚本执行adb命令
在Hudson或者Jenkins中利用ant的exec 来调用shell命令,通过shell脚本来执行adb shell命令,可以正常执行,不会出现在ant中直接调用adb shell出现的假死情况. ...
- 最完整的自动化测试流程:Python编写执行测试用例及定时自动发送最新测试报告邮件
今天笔者就要归纳总结下一整套测试流程,从无到有,实现零突破,包括如何编写测试用例,定时执行测试用例,查找最新生成的测试报告文件,自动发送最新测试报告邮件,一整套完整的测试流程.以后各位只要着重如何编写 ...
- 16级第一周寒假作业F题
Subsequence TimeLimit:1000MS MemoryLimit:65536K 64-bit integer IO format:%lld Problem Description A ...
- linux下的安装百度云网盘
linux下的百度网盘 (2014-10-20 18:01:14) 标签: linux 百度网盘 网盘 百度 forlinux 分类: 技术博文 百度网盘说实话,其实我挺喜欢的,好处什么的,就不说了, ...