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 集合的大小,需要查询 ...
随机推荐
- C# winform 只运行一个应用程序
应用程序只有一个实例,当启动一次时创建实例,当多次启用时激活当前实例. 创建一个单利管理类 using Microsoft.VisualBasic.ApplicationServices; publi ...
- winform messagebox自动关闭
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 转: c# 字符串公式计算
C# 自动计算字符串公式的值(三种方式) 从网络上找到这段源码,重新整理后测试通过. 有三种方式可自动计算字符串公式的值:1. 最简单的方式,由SQL语句计算2. 使用Microsoft.Javasc ...
- [翻译] CNPPopupController
CNPPopupController CNPPopupController is a simple and versatile class for presenting a custom popup ...
- 铁乐学python_Day42_锁和队列
铁乐学python_Day42_锁和队列 例:多个线程抢占资源的情况 from threading import Thread import time def work(): global n tem ...
- 铁乐学python_Day39_多进程和multiprocess模块2
铁乐学python_Day39_多进程和multiprocess模块2 锁 -- multiprocess.Lock (进程同步) 之前我们千方百计实现了程序的异步,让多个任务可以同时在几个进程中并发 ...
- jupyter notebook设置主题背景,字体和扩展插件
windows上安装Anaconda (IPython notebook) Anaconda是一个包与环境的管理器,一个Python发行版,以及一个超过1000多个开源包的集合.它是免费和易于安装的, ...
- Java & Groovy & Scala & Kotlin - 20.Switch 与模式匹配
Overview 本章主要介绍高级条件语句中的 switch 语句以及其增强版的模式匹配. Java 篇 Switch 特点 Java 中 switch 语句功能类似 if,但是 switch 主要用 ...
- 1026. [SCOI2009]windy数【数位DP】
Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道, 在A和B之间,包括A和B,总共有多少个windy数? I ...
- Asp.net MVC 控制器扩展方法实现jsonp
项目需要,在使用KendoUI,又涉及到jsonp数据格式的处理,网上看到这样一种实现方法,在此小记一下(其实是因为公司里只能上博客园等少数网站,怕自己忘了,好查看一下,哈哈哈) 1. 新建控制器扩展 ...