2555: SubString
2555: SubString
题意:
动态在末尾加入一个字符串,询问一个字符串出现了多少次。
分析:
如果没有动态加入,那么建出SAM后,求出parent树上,每个点|Right|,然后走一遍找到对应的点,这个点的Right集合的大小就是答案。
求Right可以从叶子结点往上走一遍。
考虑动态加入,那么会在parent树上,增加一点,并且支持加边删边,求一个点的权值,在一条到根的链上增加一个数,所以LCT维护parent树。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; struct LCT{
int fa[N], ch[N][], sk[N], top, tag[N], R[N];
inline void add(int x,int v) {
if (x) R[x] += v, tag[x] += v;
}
inline void pushdown(int x) {
if (tag[x]) add(ch[x][], tag[x]), add(ch[x][], tag[x]), tag[x] = ;
}
inline bool isroot(int x) {
return ch[fa[x]][] != x && ch[fa[x]][] != x;
}
inline int son(int x) {
return ch[fa[x]][] == x;
}
inline void rotate(int x) {
int y = fa[x], z = fa[y], c = son(y), b = son(x), a = ch[x][!b];
if (!isroot(y)) ch[z][c] = x; fa[x] = z;
ch[x][!b] = y; fa[y] = x;
ch[y][b] = a; if (a) fa[a] = y;
}
inline void splay(int x) {
top = , sk[] = x;
for (int i = x; !isroot(i); i = fa[i]) sk[++top] = fa[i];
while (top) pushdown(sk[top --]);
while (!isroot(x)) {
int y = fa[x];
if (isroot(y)) rotate(x);
else {
if (son(x) == son(y)) rotate(y), rotate(x);
else rotate(x), rotate(x);
}
}
}
inline void access(int x) {
for (int last = ; x; last = x, x = fa[x]) {
splay(x); ch[x][] = last;
}
}
inline void link(int x,int y) { // 这是一棵有根树,每条边的方向确定(指向父节点),不需要makeroot,后面是取出这条链,打标记
fa[x] = y; access(y); splay(y); add(y, R[x]);
}
inline void cut(int x) { // x 和 fa[x] 断开,fa[x]的深度小,所以是x的左儿子
access(x); splay(x); add(ch[x][], -R[x]);
fa[ch[x][]] = ; ch[x][] = ;
}
}lct; int ch[N][], len[N], fa[N], Last = , Index = ;
void extend(int c) {
int np = ++Index, p = Last; lct.R[np] = ;
for (; p && !ch[p][c]; p = fa[p]) ch[p][c] = np;
if (!p) fa[np] = , lct.link(np, );
else {
int Q = ch[p][c];
if (len[Q] == len[p] + ) fa[np] = Q, lct.link(np, Q);
else {
int NQ = ++Index;
fa[NQ] = fa[Q]; lct.link(NQ, fa[Q]);
len[NQ] = len[p] + ;
memcpy(ch[NQ], ch[Q], sizeof ch[Q]);
fa[Q] = fa[np] = NQ;
lct.cut(Q); lct.link(Q, NQ); lct.link(np, NQ);
for (; p && ch[p][c] == Q; p = fa[p]) ch[p][c] = NQ;
}
}
Last = np;
}
void build(char *s,int len) {
for (int i = ; i < len; ++i) extend(s[i] - 'A');
}
int find(char *s,int len) {
int now = ;
for (int i = ; i < len; ++i) {
now = ch[now][s[i] - 'A'];
if (!now) return ;
}
lct.splay(now);
return lct.R[now];
}
void getstr(char *s,int x,int len) {
for (int i = ; i < len; ++i) {
x = (x * + i) % len;
swap(s[i], s[x]);
}
}
char s[N], opt[];
int main() {
int n = read(), L, lastans = , ans;
scanf("%s", s);
L = strlen(s);
build(s, L);
for (int i = ; i <= n; ++i) {
scanf("%s%s", opt, s);
L = strlen(s);
getstr(s, lastans, L);
if (opt[] == 'Q') {
ans = find(s, L); lastans ^= ans;
printf("%d\n", ans);
}
else build(s, L);
}
return ;
}
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 懒得写背景了 ...
- 2555: SubString[LCT+SAM]
2555: SubString Time Limit: 30 Sec Memory Limit: 512 MB Submit: 2601 Solved: 780 [Submit][Status][ ...
- 【BZOJ 2555】 2555: SubString (SAM+LCT)
2555: SubString Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 2548 Solved: 762 Description 懒得写背景了 ...
- 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后的到的状态 ...
- 【BZOJ】2555: SubString(后缀自动机)
http://www.lydsy.com/JudgeOnline/problem.php?id=2555 学到了如何快速维护right值orz (不过这仍然是暴力维护,可以卡到O(n) 首先我们在加一 ...
- bzoj 2555 SubString——后缀自动机+LCT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2555 要维护 right 集合的大小.因为 fa 会变,且 fa 构成一棵树,所以考虑用 L ...
- bzoj 2555 SubString —— 后缀自动机+LCT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2555 建立后缀自动机,就可以直接加入新串了: 出现次数就是 Right 集合的大小,需要查询 ...
随机推荐
- Jboss Jmx-Console和 Jboss web-console安全设置
1.介绍 如果你暴露你的JBoss服务器通过网络(如通过启动服务器使用选项B 0.0.0.0或者通过改变jboss.bind.address首次出现0.0.0.0在.../jboss/server/d ...
- vs2017 调试时出现 cannot connect to runtime process错误
用Visual Studio 2017 .net core进行开发时 ,调试运行项目时出现如下错误 解决方案,调试>选项,取消勾选,关闭对JavaScript的调试
- "docker build" requires exactly 1 argument(s).
Docker 是怎么样的东西,这里就不说了,这里说说dockerfile创建容器时遇到的问题. 首先我想达到的目的很简单,就是用dockerfile去创建容器,步骤如下: 创建并编辑dockerfil ...
- c基础_笔记_1
定义变量 int i; 也可以 int i,num; 赋值,c必须先定义变量再赋值 num = 0; 循环for for(i=1; i<=0; i++) { printf("%d \n ...
- (1)抽象类 (2)接口 (3)内部类 (4)Object类
1.抽象类(重点)1.1 抽象方法的概念 抽象方法就是指不能具体实现的方法,也就是该方法没有方法体,使用abstract关键字修饰如: public abstract void cry(); 1.2 ...
- BZOJ 1040 骑士 基环树 树形DP
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1040 题目大意: Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫 ...
- Oracle从一个用户导出数据到另一个用户
如果想导入的用户已经存在: 1. 导出用户 expdp user1/pass1 directory=dumpdir dumpfile=user1.dmp 2. 导入用户 impdp user2/pas ...
- 常用的npm命令
npm ls -g 列出全局安装的所有模块 npm ls webpack -g 查看全局安装的模块版本信息 npm view webpack versions 查看npm服务器上的全部版本信息 npm ...
- 3192: [JLOI2013]删除物品
3192: [JLOI2013]删除物品 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1366 Solved: 794 [Submit][Statu ...
- luogu P2709 小B的询问
嘟嘟嘟 莫队板子. 记住:删除是先删除再移动,添加是先移动在添加! #include<cstdio> #include<iostream> #include<cmath& ...