【刷题】BZOJ 2555 SubString
Description
懒得写背景了,给你一个字符串init,要求你支持两个操作
(1):在当前字符串的后面插入一个字符串
(2):询问字符串s在当前字符串中出现了几次?(作为连续子串)
你必须在线支持这些操作。
Input
第一行一个数Q表示操作个数
第二行一个字符串表示初始字符串init
接下来Q行,每行2个字符串Type,Str
Type是ADD的话表示在后面插入字符串。
Type是QUERY的话表示询问某字符串在当前字符串中出现了几次。
为了体现在线操作,你需要维护一个变量mask,初始值为0
读入串Str之后,使用这个过程将之解码成真正询问的串TrueStr。
询问的时候,对TrueStr询问后输出一行答案Result
然后mask=maskxorResult
插入的时候,将TrueStr插到当前字符串后面即可。
HINT:ADD和QUERY操作的字符串都需要解压
长度 <= 600000,询问次数<= 10000,询问总长度<= 3000000
新加数据一组--2015.05.20
Sample Input
2
A
QUERY B
ADD BBABBBBAAB
Sample Output
0
Solution
LCT+SAM
考虑静态时,一个字符串 \(S\) 在另一个字符串 \(T\) 中的出现次数。将 \(T\) 建出SAM,那么 \(S\) 出现次数就是先将 \(S\) 在SAM上匹配,然后匹配完的那个节点的size就是出现次数了。
那么动态的的话,用LCT维护parent树就好了
#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=600000+10,MAXM=3000000+10;
int n,q,ch[MAXN<<1][30],fa[MAXN<<1],len[MAXN<<1],size[MAXN<<1],mask,las=1,tot=1;
char s[MAXM];
#define lc(x) ch[(x)][0]
#define rc(x) ch[(x)][1]
struct LinkCut_Tree{
int sum[MAXN<<1],ch[MAXN<<1][2],fa[MAXN<<1],stack[MAXN<<1],cnt,Isum[MAXN<<1],rev[MAXN<<1];
inline bool nroot(int x)
{
return lc(fa[x])==x||rc(fa[x])==x;
}
inline void reverse(int x)
{
std::swap(lc(x),rc(x));
rev[x]^=1;
}
inline void pushup(int x)
{
sum[x]=sum[lc(x)]+sum[rc(x)]+size[x]+Isum[x];
}
inline void pushdown(int x)
{
if(rev[x])
{
if(lc(x))reverse(lc(x));
if(rc(x))reverse(rc(x));
rev[x]=0;
}
}
inline void rotate(int x)
{
int f=fa[x],p=fa[f],c=(rc(f)==x);
if(nroot(f))ch[p][rc(p)==f]=x;
fa[ch[f][c]=ch[x][c^1]]=f;
fa[ch[x][c^1]=f]=x;
fa[x]=p;
pushup(f);
pushup(x);
}
inline void splay(int x)
{
cnt=0;
stack[++cnt]=x;
for(register int i=x;nroot(i);i=fa[i])stack[++cnt]=fa[i];
while(cnt)pushdown(stack[cnt--]);
for(register int y=fa[x];nroot(x);rotate(x),y=fa[x])
if(nroot(y))rotate((lc(y)==x)==(lc(fa[y])==y)?y:x);
pushup(x);
}
inline void access(int x)
{
for(register int y=0;x;x=fa[y=x])
{
splay(x);
Isum[x]+=sum[rc(x)];
rc(x)=y;
Isum[x]-=sum[rc(x)];
pushup(x);
}
}
inline void makeroot(int x)
{
access(x);splay(x);reverse(x);
}
inline void split(int x,int y)
{
makeroot(x);access(y);splay(y);
}
inline void link(int x,int y)
{
makeroot(x);access(y);splay(y);
fa[x]=y;
Isum[y]+=sum[x];
pushup(y);
}
inline void cut(int x,int y)
{
split(x,y);
fa[x]=lc(y)=0;
pushup(y);
}
};
LinkCut_Tree T;
#undef lc
#undef rc
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void extend(int c)
{
int p=las,np=++tot;
las=np;
len[np]=len[p]+1;size[np]=1;
while(p&&!ch[p][c])ch[p][c]=np,p=fa[p];
if(!p)fa[np]=1,T.link(np,1);
else
{
int q=ch[p][c];
if(len[q]==len[p]+1)fa[np]=q,T.link(np,q);
else
{
int nq=++tot;
fa[nq]=fa[q];T.link(nq,fa[q]);
memcpy(ch[nq],ch[q],sizeof(ch[nq]));
len[nq]=len[p]+1;T.cut(q,fa[q]);
fa[q]=fa[np]=nq;T.link(q,nq);T.link(np,nq);
while(p&&ch[p][c]==q)ch[p][c]=nq,p=fa[p];
}
}
}
inline void decode(int mask)
{
for(register int i=0,lt=strlen(s);i<lt;++i)std::swap(s[i],s[mask=(mask*131+i)%lt]);
}
int main()
{
read(q);
scanf("%s",s);
for(register int i=0,lt=strlen(s);i<lt;++i)extend(s[i]-'A'+1);
while(q--)
{
char opt[8];scanf("%s",opt);scanf("%s",s);
decode(mask);
if(opt[0]=='A')
for(register int i=0,lt=strlen(s);i<lt;++i)extend(s[i]-'A'+1);
if(opt[0]=='Q')
{
int j=1,mark=1;
for(register int i=0,lt=strlen(s),c;i<lt;++i)
{
c=s[i]-'A'+1;
if(ch[j][c])j=ch[j][c];
else {mark=0;break;}
}
if(!mark)puts("0");
else
{
T.makeroot(1);T.access(j);
write(T.Isum[j]+size[j],'\n');
mask^=(T.Isum[j]+size[j]);
}
}
}
return 0;
}
【刷题】BZOJ 2555 SubString的更多相关文章
- bzoj 2555: SubString 后缀自动机+LCT
2555: SubString Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 688 Solved: 235[Submit][Status][Dis ...
- 字符串(LCT,后缀自动机):BZOJ 2555 SubString
2555: SubString Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1620 Solved: 471 Description 懒得写背景了 ...
- ●BZOJ 2555 SubString
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2555题解: 后缀自动机+LCT 不难发现,对于输入的询问串,在自动机里trans后的到的状态 ...
- bzoj 2555 SubString(SAM+LCT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2555 [题意] 给定一个字符串,可以随时插入字符串,提供查询s在其中作为连续子串的出现 ...
- bzoj 2555: SubString
Description 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支 ...
- bzoj 2555 SubString——后缀自动机+LCT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2555 要维护 right 集合的大小.因为 fa 会变,且 fa 构成一棵树,所以考虑用 L ...
- 【leetcode刷题笔记】Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...
- bzoj 2555 SubString —— 后缀自动机+LCT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2555 建立后缀自动机,就可以直接加入新串了: 出现次数就是 Right 集合的大小,需要查询 ...
- BZOJ 2555 SubString(LCT+后缀树)
喜闻乐见的LCT+SAM 此题要求动态插入,直接上后缀树.然后询问其实就是求一个节点的子树后缀结束节点的个数. 因为建立后缀树需要插入和删除,就直接上LCT.每次加入一个点,把它到根的路径加一 (现在 ...
随机推荐
- scala : 类型与类
scala类型系统:1) 类型与类 在Java里,一直到jdk1.5之前,我们说一个对象的类型(type),都与它的class是一一映射的,通过获取它们的class对象,比如 String.class ...
- WPF DataGridTable
由于项目要显示表头合并,而数据源列随时变更,又不想重复的画表格,就实现动态数据(dynamic)绑定和配置数据列模板的方式 编辑DataGridColumnHeader样式实现表头合并:效果如下 实现 ...
- Python中的装饰器的使用及固定模式
装饰器的使用: 在不想修改函数的调用方式,但是想给函数添加内容的功能的时候使用 为什么使用装饰器: 软件实体应该是可扩展,而不可修改的.也就是说,对扩展是开放的,而对修改是封闭的. 因此,引出 ...
- Pairs Forming LCM LightOJ - 1236 素因子分解
Find the result of the following code: long long pairsFormLCM( int n ) { long long res = 0; fo ...
- sqli-labs学习笔记 DAY1
DAY 1 准备工作 安装phpstudy 安装配置sqli-labs 学习笔记 SQL语句的注释:–, # +在URL经过编码后会编码为空格 SQL语句的查询语句:SELECT column_nam ...
- Bin Packing 装箱问题——NPH问题的暴力枚举 状压DP
题目: 给定n(1≤n≤24)个物品,重量分别为wi,装进一些容量为S(S<1e8)的背包,最少需要多少个背包?
- [C++] Solve "No source available for main()" error when debugging on Eclipse
In Mac, the issue image: 1. A existing cmake project on disk 2. import this project into Eclipse. 3 ...
- linux计划任务 学习笔记
原文链接: http://www.tsingfeng.com/?tag=cronjob 本文说的计划任务是指linux的Cronjob.语法 下面是个简单的计划任务: 10 * * * * /usr/ ...
- 欢迎来怼第二周Scrum会议六(总第十三次)
一.小组信息 队名:欢迎来怼小组成员队长:田继平成员:李圆圆,葛美义,王伟东,姜珊,邵朔,冉华 小组照片 二.开会信息 时间:2017/10/25 17:19~17:35(总计16min).地点:东 ...
- 前端获取URL和SESSON中的值
.CS中代码 public ActionResult Index(string viewname, bool partial = false) { //获取URL中的 foreach (var key ...