【BZOJ2555】SubString
算是学会sam了吧……
原题:
懒得写背景了,给你一个字符串init,要求你支持两个操作
(1):在当前字符串的后面插入一个字符串
(2):询问字符串s在当前字符串中出现了几次?(作为连续子串)
你必须在线支持这些操作。
字符串最终长度 <= 600000,询问次数<= 10000,询问总长度<= 3000000
恩
因为sam添点本来就是动态的所以动态往后连接串就不用管了
问题是维护right集的大小,如果新添了一个点np那么从np一直到sam的根right集的大小都会+1对吧
暴力修改即可?以前是可以的,现在新添一组数据卡掉了……
那咋办,动态树上进行路径修改,lct口贝
值得注意的是修改的路径是当前点到原树根的,lct上修改的时候需要把原树根钦定为lct的根然后再access再splay再修改
注意询问串可能比插入串和起始串长,如果共用一个字符数组的话要开成3e6……
注意题中给出的强制在线函数中mask是个局部变量……也就是说转码的时候mask不会变,询问的时候才会变……
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int qwq=;
int m; char s[],t[]; int n;
int nxt[][],fth[],mx[],sz[],lst=,npt=;
int c[][],fa[],v[],dt[],rvs[];
int stck[],tp=;
inline bool isrt(int x){ return (c[fa[x]][]!=x)&(c[fa[x]][]!=x);}
inline void pshd(int x){
v[c[x][]]+=dt[x],dt[c[x][]]+=dt[x];
v[c[x][]]+=dt[x],dt[c[x][]]+=dt[x];
dt[x]=;
if(!rvs[x]) return ;
rvs[c[x][]]^=,rvs[c[x][]]^=,rvs[x]=;
swap(c[x][],c[x][]);
}
inline void rtt(int x){
int y=fa[x],z=fa[fa[x]],l,r;
r=(c[y][]==x); l=r^;
if(!isrt(y)) c[z][c[z][]==y]=x;
fa[x]=z,fa[y]=x,fa[c[x][r]]=y;
c[y][l]=c[x][r],c[x][r]=y;
}
inline void sply(int x){
stck[tp=]=x;
for(int i=x;!isrt(i);i=fa[i]) stck[++tp]=fa[i];
while(tp) pshd(stck[tp--]);
while(!isrt(x)){
if(!isrt(fa[x])) rtt((c[fa[x]][]==x)^(c[fa[fa[x]]][]==fa[x])?x:fa[x]);
rtt(x);
}
}
inline void accs(int x){ for(int i=;x;sply(x),c[x][]=i,x=fa[i=x]);}
inline void qdrt(int x){ accs(x),sply(x),rvs[x]^=;}
inline void lk(int x,int y){ qdrt(x),fa[x]=y,sply(x);}
inline void ct(int x,int y){ qdrt(x),accs(y),sply(y),fa[x]=c[y][]=;}
inline void bf(int x,int y){ qdrt(),accs(x),sply(x),v[x]+=y,dt[x]+=y;}
inline int sch(int x){ sply(x); return v[x];}
void rds(){
scanf("%s",s); n=strlen(s); int tmp=qwq;
for(int i=;i<n;++i){
tmp=(tmp*+i)%n;
swap(s[i],s[tmp]);
}
}
/*void ist(int x){
int p=lst,np=lst=++npt;
mx[np]=mx[p]+1; sz[np]=0;
while((!nxt[p][x])&((!p)^1)) nxt[p][x]=np,p=fth[p];
if(!p) fth[np]=1;
else{
int q=nxt[p][x];
if(mx[q]==mx[p]+1) fth[np]=q;
else{
int nq=++npt; mx[nq]=mx[p]+1; sz[nq]=sz[q];
memcpy(nxt[nq],nxt[q],sizeof(nxt[q]));
fth[nq]=fth[q],fth[q]=fth[np]=nq;
while(nxt[p][x]==q) nxt[p][x]=nq,p=fth[p];
}
}
while(np) ++sz[np],np=fth[np];
}*/
void ist(int x){
int p=lst,np=lst=++npt;
mx[np]=mx[p]+; v[np]=;
while((!nxt[p][x])&((!p)^)) nxt[p][x]=np,p=fth[p];
if(!p) fth[np]=,lk(np,);
else{
int q=nxt[p][x];
if(mx[q]==mx[p]+) fth[np]=q,lk(np,q);
else{
int nq=++npt; mx[nq]=mx[p]+; v[nq]=sch(q);
memcpy(nxt[nq],nxt[q],sizeof(nxt[q]));
ct(q,fth[q]),lk(nq,fth[q]),lk(q,nq),lk(np,nq);
fth[nq]=fth[q],fth[q]=fth[np]=nq;
while(nxt[p][x]==q) nxt[p][x]=nq,p=fth[p];
}
}
bf(np,);
//cout<<sch(5)<<" "<<fth[3]<<endl;
}
int qr(){
int tmp=;
for(int i=;i<n;++i) tmp=nxt[tmp][s[i]-'A'];
return tmp?sch(tmp):;
}
int main(){freopen("ddd.in","r",stdin);
cin>>m; scanf("%s",s); n=strlen(s);
for(int i=;i<n;++i) ist(s[i]-'A');
while(m--){
scanf("%s",t); rds();
if(t[]=='A') for(int i=;i<n;++i) ist(s[i]-'A');
else{ int tmp=qr(); qwq^=tmp; printf("%d\n",tmp);}
}
return ;
}
【BZOJ2555】SubString的更多相关文章
- 【BZOJ2555】SubString(后缀自动机,Link-Cut Tree)
[BZOJ2555]SubString(后缀自动机,Link-Cut Tree) 题面 BZOJ 题解 这题看起来不难 每次要求的就是\(right/endpos\)集合的大小 所以搞一个\(LCT\ ...
- 【BZOJ2555】SubString 后缀自动机+LCT
[BZOJ2555]SubString Description 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2 ...
- 【BZOJ-2555】SubString 后缀自动机 + LinkCutTree
2555: SubString Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1936 Solved: 551[Submit][Status][Di ...
- 【bzoj2555】 SubString
http://www.lydsy.com/JudgeOnline/problem.php?id=2555 (题目链接) 题意 给出一个初始串,维护两个操作.在原串后面加入一个字符串:询问某个字符串在原 ...
- 【BZOJ2555】SubString(后缀自动机,LCT)
题意:给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支持这些操作. 长度 <= ...
- 【leetcode】Substring with Concatenation of All Words
Substring with Concatenation of All Words You are given a string, S, and a list of words, L, that ar ...
- 【leetcode】Substring with Concatenation of All Words (hard) ★
You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
随机推荐
- jQuery validator plugin之Plugin Method
原文 .validate() validate( [options ] ) options Type: Object debug (default: false) Type: Boolean Enab ...
- 字段值为 null 时,序列化或反序列化成其他值
using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System; using System.Collections.G ...
- 第八届蓝桥杯省赛 K倍区间
问题描述 给定一个长度为N的数列,A1, A2, ... AN,如果其中一段连续的子序列Ai, Ai+1, ... Aj(i <= j)之和是K的倍数,我们就称这个区间[i, j]是K倍区间. ...
- 组合,多态与多态性,封装以及property装饰器介绍
一:组合: 什么是组合:组合指的是某一个对象拥有一个属性,该属性的值是另外一个类的对象. 为何要用组合:通过为某一个对象添加属性(属性的值是另外一个类的对象)的方式,可以间接地将两个类关联/整合/组合 ...
- PHP冒泡排序-手写
<?php $a = [1,3,5,2,9,6]; for ($i = 0 ;$i < count($a) ;$i++) { for ($j = $i + 1;$j < count( ...
- Redhat中关于httpd仓库安装的简要步骤
创建repo-server: yum install httpd yum install httpd -y < -y 表示在安装过程中与界面交互时自动答复yes >sys ...
- jenkins配置QQ邮箱自动发送RF测试构建结果通知邮件
声明:转载请注明出处,谢谢 首先确认QQ邮箱SMTP服务器的地址和端口号.如下图所示,请谨记,JENKINS全局邮箱配置需要使用: 步骤1:开启QQ邮箱的smtp服务:登陆QQ邮箱-设置-账户-开启P ...
- oracle数据库 concat 与 ||
1.简述: 通过几条sql语句来引入此问题 先新建一张表,插入几条数据,如下: CREATE TABLE tab1 (col1 VARCHAR2(6), col2 CHAR(6), col3 VARC ...
- C# wnform 请求http ( get , post 两种方式 )
1.Get请求 string strURL = "http://localhost/WinformSubmit.php?tel=11111&name=张三";System. ...
- hadoop集群中动态添加节点
集群的性能问题需要增加服务器节点以提高整体性能 https://www.cnblogs.com/fefjay/p/6048269.html hadoop集群之间hdfs文件复制 https://www ...