BZOJ 2555: SubString 后缀自动机_LCT
很水的一道题,就是有些细节没注意到.
比如说将调试信息误以为是最终结果而多调了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的更多相关文章
- 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 ...
- 字符串(LCT,后缀自动机):BZOJ 2555 SubString
2555: SubString Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1620 Solved: 471 Description 懒得写背景了 ...
- bzoj 2555: SubString【后缀自动机+LCT】
一直WA--找了半天错的发现居然是解密那里的mask其实是不能动的--传进去的会变,但是真实的那个不会变-- 然后就是后缀自动机,用LCT维护parent树了--注意不能makeroot,因为自动机的 ...
- bzoj 2555 SubString(SAM+LCT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2555 [题意] 给定一个字符串,可以随时插入字符串,提供查询s在其中作为连续子串的出现 ...
- ●BZOJ 2555 SubString
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2555题解: 后缀自动机+LCT 不难发现,对于输入的询问串,在自动机里trans后的到的状态 ...
- SPOJ1811 LCS - Longest Common Substring(后缀自动机)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- 【BZOJ-2555】SubString 后缀自动机 + LinkCutTree
2555: SubString Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1936 Solved: 551[Submit][Status][Di ...
随机推荐
- 01 C#基础
第一天 .net平台(中国移动互联网平台): .net框架(信号塔): CLR(公共语言运行时) .Net类 库 我们使用的语言是——C# 2.解决方案项目与类的关系: 解决方案:公司 项目:部门 类 ...
- ./configure --prefix /?/? 解释
-- ./configure :编译源码 --prefix :把所有资源文件放在 之后的路径之后
- [luogu] P2519 [HAOI2011]problem a (贪心)
P2519 [HAOI2011]problem a 题目描述 一次考试共有n个人参加,第i个人说:"有ai个人分数比我高,bi个人分数比我低."问最少有几个人没有说真话(可能有相同 ...
- Golang-and-package-version-managment
参考文章 学习Golang之后对golang中的版本管理,包管理等机制一直没有很好的琢磨,偶然想起还是觉得很有必要进行归纳,包管理使用起来简单,无非就是install,uninstall,list等, ...
- git与github的连接流程
https://blog.csdn.net/sssssuuuuu666/article/details/78565381 https://www.cnblogs.com/wzd5230/p/49064 ...
- 【codeforces 734F】Anton and School
[题目链接]:http://codeforces.com/problemset/problem/734/F [题意] 给你两个数组b和c; 然后让你找出一个非负数组a满足题中所给关系; [题解] 有个 ...
- linux内核(四)内存管理单元MMU
1,基本概念 一个程序运行时没必要全部都同时装入内存,只需要把当前需要运行的部分装入内存即可,这样就使得一个大程序可以在较小的内存中运行,也使得内存中可以同时装入更多的程序并发执行,从用户角度看,该系 ...
- Windows系统环境变量、JAVA环境变量配置以及JVM加载过程
一:用户变量和系统变量的区别 右击我的电脑.属性.高级系统设置.环境变量. 对话框的上面为Administrator的用户变量,对话框的下面为系统变量.我们所说的环境变量一般指系统环境变量,对所有用户 ...
- Linux文件查找命令具体解释-which whereis find locate
原创BLog.转载请注明出处 http://blog.csdn.net/hello_hwc? viewmode=contents which命令 首先查看man which的说明 which - sh ...
- 我是怎么从项目中的lib加JAR更换为maven管理的
原来我对maven的使用应该还是去年的时候吧,当时对maven并不感冒(请不要吐槽哈),认为为什么一定要用maven来管理呢,我自己管理jar不是一样么,当时还认为自己管理jar还各种方便还对mave ...