很水的一道题,就是有些细节没注意到.

比如说将调试信息误以为是最终结果而多调了20分钟QAQ .....

我们注意到,每新加一个节点,改变的是该节点沿着 Parent 走一直走到根节点.

对应的,在 LCT 上进行修改即可.

改变一个节点的 Parent,就对应 cut

断掉原边后将新边连接即可.

Code:

#include <cstdio>
#include <algorithm>
#include <cstring>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 2000009
#define N 30
using namespace std;
int result,query,n,M;
char s[3000009]; void Decode(int mask){
int nn=strlen(s);
for(int j=0;j<nn;j++){
mask=(mask*131+j)%nn;
swap(s[j],s[mask]);
}
}
struct Link_Cut_Tree{
int ch[maxn][2],f[maxn],tag[maxn],sta[maxn],val[maxn];
int get(int x) {return ch[f[x]][1]==x; }
int which(int x){ return ch[f[x]][1]==x;}
int isRoot(int x){ return !(ch[f[x]][1]==x||ch[f[x]][0]==x);}
int lson(int x){ return ch[x][0];}
int rson(int x){return ch[x][1];}
void add(int x,int delta){if(!x)return;val[x]+=delta,tag[x]+=delta;}
void pushdown(int x){if(tag[x]) add(lson(x),tag[x]),add(rson(x),tag[x]),tag[x]=0;}
void rotate(int x){
int old=f[x],fold=f[old],which=get(x);
if(!isRoot(old)) ch[fold][ch[fold][1]==old]=x;
ch[old][which]=ch[x][which^1],f[ch[old][which]]=old;
ch[x][which^1]=old,f[old]=x,f[x]=fold;
}
void splay(int x){
int v=0,u=x;
sta[++v]=u;
while(!isRoot(u)) sta[++v]=f[u],u=f[u];
while(v) pushdown(sta[v--]);
u=f[u];
for(int fa;(fa=f[x])!=u;rotate(x))
if(f[fa]!=u) rotate(get(fa)==get(x)?fa:x);
}
void Access(int x){
for(int y=0;x;y=x,x=f[x])
splay(x),ch[x][1]=y;
}
void link(int a,int b){
Access(a),splay(a),add(a,val[b]),f[b]=a;
}
void cut(int b){
Access(b),splay(b);
add(lson(b),-val[b]),f[lson(b)]=ch[b][0]=0;
}
}tree;
struct Suffix_Automaton{
int last,tot,dis[maxn],ch[maxn][N];
int f[maxn];
void init(){last=tot=1;}
void ins(int c){
int p=last,np=++tot; last=np; dis[np]=dis[p]+1;tree.val[np]=tree.tag[np]=1;
while(p&&!ch[p][c])ch[p][c]=np,p=f[p];
if(!p) f[np]=1,tree.link(1,np);
else{
int q=ch[p][c],nq;
if(dis[q]==dis[p]+1)f[np]=q,tree.link(q,np);
else{
nq=++tot;tree.val[nq]=0;
dis[nq]=dis[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
//printf("%d\n",q)
f[nq]=f[q];
tree.link(f[q],nq),tree.cut(q),tree.link(nq,q),tree.link(nq,np);
f[q]=f[np]=nq;
while(p&&ch[p][c]==q) ch[p][c]=nq,p=f[p];
}
}
}
int search_str(){
n=strlen(s);
int p = 1;
for(int i=0;i<n;++i) {
if(!ch[p][s[i]-'A']) return -1;
p=ch[p][s[i]-'A'];
}
return p;
}
}sam;
int main(){
//setIO("input");
sam.init(),scanf("%d%s",&query,s),n=strlen(s);
for(int i=0;i<n;++i) sam.ins(s[i]-'A');
char opt[10]; int u;
while(query--){
scanf("%s%s",opt,s);
Decode(M);
if(opt[0]=='A') {
n=strlen(s);
for(int i=0;i<n;++i) sam.ins(s[i]-'A');
}
if(opt[0]=='Q') {
u=sam.search_str();
if(u==-1) result=0;
else tree.Access(u),tree.splay(u),result=tree.val[u];
M^=result;
printf("%d\n",result);
}
}
return 0;
}

  

BZOJ 2555: SubString 后缀自动机_LCT的更多相关文章

  1. bzoj 2555 SubString —— 后缀自动机+LCT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2555 建立后缀自动机,就可以直接加入新串了: 出现次数就是 Right 集合的大小,需要查询 ...

  2. bzoj 2555: SubString 后缀自动机+LCT

    2555: SubString Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 688  Solved: 235[Submit][Status][Dis ...

  3. bzoj 2555 SubString——后缀自动机+LCT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2555 要维护 right 集合的大小.因为 fa 会变,且 fa 构成一棵树,所以考虑用 L ...

  4. 字符串(LCT,后缀自动机):BZOJ 2555 SubString

    2555: SubString Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1620  Solved: 471 Description 懒得写背景了 ...

  5. bzoj 2555: SubString【后缀自动机+LCT】

    一直WA--找了半天错的发现居然是解密那里的mask其实是不能动的--传进去的会变,但是真实的那个不会变-- 然后就是后缀自动机,用LCT维护parent树了--注意不能makeroot,因为自动机的 ...

  6. bzoj 2555 SubString(SAM+LCT)

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

  7. ●BZOJ 2555 SubString

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2555题解: 后缀自动机+LCT 不难发现,对于输入的询问串,在自动机里trans后的到的状态 ...

  8. SPOJ1811 LCS - Longest Common Substring(后缀自动机)

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

  9. 【BZOJ-2555】SubString 后缀自动机 + LinkCutTree

    2555: SubString Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1936  Solved: 551[Submit][Status][Di ...

随机推荐

  1. X Macro

    30年前我念大学时从一个朋友那里学来的一个技巧. 它是汇编语言的一个宏,但很容易转换为C语言宏. 我一直在使用它,但有意思的是我还从没在别人的代码中看到过.现在该我把这个小技巧传递下去了. 让我们举个 ...

  2. js 基本基础知识回顾

    js中的一切的变量.函数.操作符等等都是区分大小写的. js的基本的数据类型->包含下面的5种: 1.undefined 2.Null 3.Boolean 4.Number 5.String j ...

  3. 关于背景颜色、TEXT、<b>、<i>、<u>、<br>、<&nbsp>、<br>、<br>、h1-h6、<span>、<div>、<ol>、<ul>、<a>标签的用法(下载、跳转、锚点)、Img插入的用法

    <html>    <head>        <meta charset="UTF-8">        <title></ ...

  4. X位的有/无符号整数

    目录 X位的有符号整数 X位的无符号整数 知识点来自leetcode整数翻转 X位的有符号整数 以4位为例,不管多少位都是相同的概念 在有符号整数中,第一位二进制位用来表示符号,0为正,1为负 最大值 ...

  5. JQ UI dialog

    初始化参数 对于 dialog 来说,首先需要进行初始化,在调用 dialog 函数的时候,如果没有传递参数,或者传递了一个对象,那么就表示在初始化一个对话框. 没有参数,表示按照默认的设置初始化对话 ...

  6. 02 C#高级

    第九天 面向过程--à面向对象 面向过程:面向的是完成这件事儿的过程,强调的是完成这件事儿的动作. 把大象塞进冰箱 1. 打开冰箱门 2. 把大象塞进去,亲下大象的屁股 3. 关闭冰箱门 孙全 瘦小 ...

  7. [置顶] openHAB 部分代码结构 UML 图

    openHAB 部分代码结构 UML 图 ModelRepository: ItemRegistry: ItemUIProvider: WebAppServlet:

  8. ES6重点知识点总结(2)

    ES6重点知识点总结(2) call和apply的作用是什么?区别是什么? call和apply的功能基本相同,都是实现继承或者转换对象指针的作用: 唯一不通的是前者参数是罗列出来的,后者是存到数组中 ...

  9. 【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] E】Short Code

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先建立一棵字典树. 显然,某一些节点上会被打上标记. 问题就转化成求所有标记的深度的和的最小值了. (标记可以上移,但是不能在同一位 ...

  10. Ubuntu 15.10 安装Qt5.5.1

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50300447 本人使用的ubuntu系 ...