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 集合的大小,需要查询 ...
随机推荐
- Man's Best Friend: The Science Behind the Dog and Human Relationship
http://info.thinkfun.com/stem-education/mans-best-friend-the-science-behind-the-dog-and-human-relati ...
- Linux nl命令详解
nl常见命令参数 nl命令在linux系统中用来计算文件中行号 -b :指定行号指定的方式,主要有两种: -b a :表示不论是否为空行,也同样列出行号(类似 cat -n): -b t :如果有空 ...
- PHP解决网站大数据大流量与高并发
1:硬件方面 普通的一个p4的服务器每天最多能支持10万左右的IP,如果访问量超过10W那么需要专用的服务器才能解决,如果硬件不给力软件怎么优化都是于事无补的.主要影响服务器的速度 有:网络-硬盘读写 ...
- 鸟哥私房菜vim常用命令
第一部份:一般模式可用的按钮说明,光标移动.复制贴上.搜寻取代等 移动光标的方法 h 或 向左箭头键(←) 光标向左移动一个字符 j 或 向下箭头键(↓) 光标向下移动一个字符 k 或 向上箭头键(↑ ...
- You may experience an access violation when you access an STL object through a pointer or reference in a different DLL or EXE
Symptoms When accessing an STL object created in one DLL or EXE through a pointer or reference in a ...
- 前段js初学总结
常用的js整理 confirm("此次修改操作会清空所有基础数据!!!您确定要修改吗?") <a onclick="delBasisData('${data['_i ...
- 阿里八八Alapa事后诸葛亮
设想和目标 1.我们的软件要解决什么问题?是否定义的很清楚?是否对典型用户和典型场景有清晰的描述? 我们的项目希望解决用户对于时间.日程管理上不够方便.直观.易丢失的问题,因为并不是新颖高端的概念,因 ...
- ABAP很厉害是怎么一种体验?
知乎上偶然看到这个问题,觉得很有意思,我也来回答一发. 我本科和研究生学的是计算机专业,做项目用C/C++,研究生三年项目的代码量大概在三到四万行左右.2007年大学毕业加入SAP成都研究院一直工作到 ...
- Mina使用总结(三)MinaClient
简单的Mina客户端代码MinaSimpleClient.java: package com.bypay.mina.client; import java.net.InetSocketAddress; ...
- redis安装出错
Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 本文为大家讲解的是Redis 2.8.18 安装报错 err ...