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 集合的大小,需要查询 ...
随机推荐
- 进程间协作---wait,notify,notifyAll
转自牛客网的一篇评论,解释的十分详细 在 Java 中,可以通过配合调用 Object 对象的 wait() 方法和 notify()方法或 notifyAll() 方法来实现线程间的通信.在线程中调 ...
- 转载:eclipse 搭建SSH项目(第一篇)
第一篇:原文地址:http://blog.csdn.net/aaaaaaaa0705/article/details/6288431(虽然没有具体的例子,不过逻辑性强点,比较容易看懂) SSH框架是最 ...
- 转:eclipse的快捷键
22 21 Eclipse中10个最有用的快捷键组合 一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到 ...
- GONMarkupParser的使用
GONMarkupParser的使用 说明 这是一个写得非常好的富文本工具类,便于你进行简易的封装.本人抛砖引玉,只进行了少量的简化使用封装. 效果 源码 https://github.com/nic ...
- codeforces 933D A Creative Cutout
题目链接 正解:组合数学. 充满套路与细节的一道题.. 首先我们显然要考虑每个点的贡献(我就不信你能把$f$给筛出来 那么对于一个点$(x,y)$,我们设$L=x^{2}+y^{2}$,那么它的贡献就 ...
- Hive学习之路 (十六)Hive分析窗口函数(四) LAG、LEAD、FIRST_VALUE和LAST_VALUE
数据准备 数据格式 cookie4.txt cookie1, ::,url2 cookie1, ::,url1 cookie1, ::,1url3 cookie1, ::,url6 cookie1, ...
- mysql报错:/usr/sbin/mysqld:unknown variable 'default-character-set=utf8'
修改mysql的字符集时,为了将默认字符集设置为utf-8,便将/etc/my.cnf下[mysqld]加了一行 default-character-set=utf8, 但是在下次启动系统的时候,my ...
- python基础整理5——多进程多线程和协程
进程与线程 1.进程 我们电脑的应用程序,都是进程,假设我们用的电脑是单核的,cpu同时只能执行一个进程.当程序处于I/O阻塞的时候,CPU如果和程序一起等待,那就太浪费了,cpu会去执行其他的程序, ...
- 最易懂的layui分页
该篇文章是在layui前端框架之分页基础上简洁化和详细化. 首先该示例采用的是Spring+MyBatis Plus+SpringMVC(常规的SSM框架),持久层换成MyBatis也行. 至于lay ...
- Docker+Kubernetes(k8s)微服务容器化实践
第1章 初识微服务微服务的入门,我们从传统的单体架构入手,看看在什么样的环境和需求下一步步走到微服务的,然后再具体了解一下什么才是微服务,让大家对微服务的概念有深入的理解.然后我们一起画一个微服务的架 ...