2555: SubString

Time Limit: 30 Sec  Memory Limit: 512 MB
Submit: 2548  Solved: 762

Description

懒得写背景了,给你一个字符串init,要求你支持两个操作

(1):在当前字符串的后面插入一个字符串

(2):询问字符串s在当前字符串中出现了几次?(作为连续子串)

你必须在线支持这些操作。

Input

第一行一个数Q表示操作个数
    
    第二行一个字符串表示初始字符串init
    
    接下来Q行,每行2个字符串Type,Str 
    
    Type是ADD的话表示在后面插入字符串。
    
    Type是QUERY的话表示询问某字符串在当前字符串中出现了几次。
    
    为了体现在线操作,你需要维护一个变量mask,初始值为0
   
    
    读入串Str之后,使用这个过程将之解码成真正询问的串TrueStr。
    询问的时候,对TrueStr询问后输出一行答案Result
    然后mask = mask xor Result  
    插入的时候,将TrueStr插到当前字符串后面即可。

HINT:ADD和QUERY操作的字符串都需要解压

Output

Sample Input

2

A

QUERY B

ADD BBABBBBAAB

Sample Output

0

HINT

40 % 的数据字符串最终长度 <= 20000,询问次数<= 1000,询问总长度<= 10000

100 % 的数据字符串最终长度 <= 600000,询问次数<= 10000,询问总长度<= 3000000

新加数据一组--2015.05.20

Source

【分析】

  一开始都没想着用LCT,后来发现我的SAM的right数组一直都是要逆拓扑序求的。

  在线的话,还要在线维护。把pre边看成一棵树,就是要支持link,cut操作的,求子树和的东西。

  其实LCT我只会做路径的,不会做子树了。看了hzwer的代码。【然后好像是,其实也是维护了路径,就是link x和f 的时候就是把f到根的路径都加上val[x]

  【ORZ欧颓果说用splay维护dfs序也可以啊?

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 600010
#define Maxl 3000010 struct node
{
int pre,son[],step;
}t[Maxn*]; struct nnode
{
int son[],fa,val,laz;
nnode() {son[]=son[]=fa=val=laz=;}
}tr[Maxn*]; char s[Maxl];
void init(int mask)
{
scanf("%s",s);
int l=strlen(s);
for(int i=;i<l;i++)
{
mask=(mask*+i)%l;
swap(s[i],s[mask]);
}
} struct lct
{
void add(int x,int y)
{
if(x) tr[x].val+=y,tr[x].laz+=y;
}
bool is_root(int x)
{
return tr[tr[x].fa].son[]!=x&&tr[tr[x].fa].son[]!=x;
}
bool pushdown(int x)
{
int lc=tr[x].son[],rc=tr[x].son[];
if(tr[x].laz)
{
add(lc,tr[x].laz);add(rc,tr[x].laz);
tr[x].laz=;
}
}
void rot(int x)
{
int fa=tr[x].fa,yy=tr[fa].fa;
int w=tr[tr[x].fa].son[]==x; if(!is_root(fa))
{
if(tr[yy].son[]==fa) tr[yy].son[]=x;
else tr[yy].son[]=x;
}tr[x].fa=yy; tr[fa].son[-w]=tr[x].son[w];
tr[tr[x].son[w]].fa=fa; tr[x].son[w]=fa;
tr[fa].fa=x;
//upd(fa);//upd(x);
}
int q[*Maxn];
void pre(int x)
{
int tp=;
while(!is_root(x)) q[++tp]=x,x=tr[x].fa;
q[++tp]=x;
for(int i=tp;i>=;i--) pushdown(q[i]);
}
void splay(int x)
{
pre(x);
while(!is_root(x))
{
int fa=tr[x].fa,yy=tr[fa].fa;
if(!is_root(fa))
{
if((tr[yy].son[]==fa)==(tr[fa].son[]==x)) rot(fa);
else rot(x);
}
rot(x);
}//upd(x);
}
void access(int x)
{
int t=;
while(x)
{
splay(x);
tr[x].son[]=t;
t=x;
x=tr[x].fa;
}
}
/*void split(int x,int y)
{
make_root(x);
access(y);
splay(y);
}*/
void link(int x,int f)
{
tr[x].fa=f;
access(f);
splay(f);
add(f,tr[x].val);
}
void cut(int x)
{
access(x);splay(x);
add(tr[x].son[],-tr[x].val);
tr[tr[x].son[]].fa=;
tr[x].son[]=;
}
}lct; int mask;
struct sam
{
int last,tot;
void extend(int k)
{
int np=++tot,p=last;
t[np].step=t[p].step+;
tr[np].val=;
while(p&&!t[p].son[k])
{
t[p].son[k]=np;
p=t[p].pre;
}
if(!p) t[np].pre=,lct.link(np,);
else
{
int q=t[p].son[k];
if(t[q].step==t[p].step+) t[np].pre=q,lct.link(np,q);
else
{
int nq=++tot;//upd(tot);
t[nq].step=t[p].step+;
memcpy(t[nq].son,t[q].son,sizeof(t[nq].son));
t[nq].pre=t[q].pre;
lct.link(nq,t[nq].pre);
t[q].pre=t[np].pre=nq;
lct.cut(q);lct.link(q,nq);lct.link(np,nq);
while(p&&t[p].son[k]==q)
{
t[p].son[k]=nq;
p=t[p].pre;
}
}
}
last=np;
}
void add()
{
init(mask);
int l=strlen(s);
for(int i=;i<l;i++) extend(s[i]-'A'+);
}
int query()
{
init(mask);
int nw=,l=strlen(s);
for(int i=;i<l;i++)
{
int ind=s[i]-'A'+;
if(!t[nw].son[ind]) return ;
nw=t[nw].son[ind];
}
lct.splay(nw);
return tr[nw].val;
}
}sam; char ss[]; int main()
{
int q;
scanf("%d",&q);
sam.tot=sam.last=;
scanf("%s",s);
int l=strlen(s);
for(int i=;i<l;i++) sam.extend(s[i]-'A'+);
while(q--)
{
scanf("%s",ss);
if(ss[]=='A') sam.add();
else
{
int ans=sam.query();
printf("%d\n",ans);
mask^=ans;
}
}
return ;
}

2017-04-19 07:48:14

【BZOJ 2555】 2555: SubString (SAM+LCT)的更多相关文章

  1. bzoj 2555 SubString(SAM+LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2555 [题意] 给定一个字符串,可以随时插入字符串,提供查询s在其中作为连续子串的出现 ...

  2. 【BZOJ 4170】 4170: 极光 (CDQ分治)

    4170: 极光 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 121  Solved: 64 Description "若是万一琪露诺(俗 ...

  3. 【BZOJ 4527】 4527: K-D-Sequence (线段树)

    4527: K-D-Sequence Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 145  Solved: 59 Description 我们称一个 ...

  4. 【BZOJ 2982】 2982: combination (卢卡斯定理)

    2982: combination Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 510  Solved: 316 Description LMZ有n个 ...

  5. 【BZOJ 3262】 3262: 陌上花开 (CDQ分治)

    3262: 陌上花开 Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A ...

  6. 【bzoj 3333】排队计划(线段树)

    n个数,求一次逆序对.接着有m次修改操作,把每次输入的位置p的数之后<=它的数取出来,从小到大排序后再放回空位里,求逆序对.(N,M<=500,000 , Ai<=10^9)思路:1 ...

  7. 2019.03.01 bzoj2555: SubString(sam+lct)

    传送门 题意简述: 要求在线支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 思路: 考虑用lctlctlct来动态维护samsa ...

  8. 【BZOJ】4721: [Noip2016]蚯蚓 / 【洛谷】P2827 蚯蚓(单调队列)

    Description 本题中,我们将用符号[c]表示对c向下取整,例如:[3.0」= [3.1」=[3.9」=3.蛐蛐国最近蚯蚓成灾了!隔壁跳 蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮 ...

  9. 【LOJ#3097】[SNOI2019]通信(费用流)

    [LOJ#3097][SNOI2019]通信(费用流) 题面 LOJ 题解 暴力就直接连\(O(n^2)\)条边. 然后分治/主席树优化连边就行了. 抄zsy代码,zsy代码是真的短 #include ...

随机推荐

  1. spring bean初始化及销毁你必须要掌握的回调方法

    spring bean在初始化和销毁的时候我们可以触发一些自定义的回调操作. 初始化的时候实现的方法 1.通过java提供的@PostConstruct注解: 2.通过实现spring提供的Initi ...

  2. TED_Topic1:Why we need to rethink capitalism

    Topic 1:Why we need to rethink capitalism By Paul Tudor Jones II # Background about our speaker      ...

  3. .NET中的异常和异常处理

    .NET中的异常(Exception) .net中的中异常的父类是Exception,大多数异常一般继承自Exception. 可以通过编写一个继承自Exception的类的方式,自定义异常类! 异常 ...

  4. 使用inline-block,使前面img,后面空div居中显示在一行后,导致当div中有内容时,div下移问题

    .pro_li img,.pro_sm{display: inline-block; *display:inline;*zoom:1;vertical-align: middle ;} 解决方法:使用 ...

  5. 项目开发 -- ZFS容量分配

    存储池 allocated 池中已实际分配的存储空间量.该属性也可通过其简短列名alloc来引用. capacity 已用的池空间百分比.此属性也可通过其简短列名cap来引用. dedupratio ...

  6. phpmywind目录结构

    phpmywind目录结构了解 admin/ 后台管理目录 admin/editor/ 后台编辑器存放目录 admin/inc/ 后台公用文件引用目录 admin/plugin/ 后台插件存放目录 a ...

  7. 017 CPU冲高定位方法

    1.通过top命令查看cpu占用高的进程ID; 2.通过top -Hp 进程ID 查看该进程下所有线程占用cpu的情况,拿出占用cpu最高的线程ID,换算成十六进制; 3.通过 jstack 进程ID ...

  8. 使用angluar-cli的ng g component home指令出现的错误

    Error: ELOOP: too many symbolic links encountered, stat '/Users/zzy/angular/taskmgr/node_modules/@an ...

  9. salt-api起不来:ImportError('No module named wsgiserver2',)

    问题:启动salt-api时没有报错但是没有端口,查看/var/log/salt/api发现如下报错: 解决方法: 下载wsgiserver2文件,放到/usr/lib64/python2.7/sit ...

  10. Eclipse的SVN插件与本地SVN客户端关联不上

    问题:当我们用SVN客户端把代码更新到本地,并导入到eclipse之后,却发现我们的SVN插件并没有起作用(没有代码入库.修改等小图标的显示,也没有check in,update等功能菜单).如果我们 ...