luogu5212/bzoj2555 substring(后缀自动机+动态树)
对字符串构建一个后缀自动机.
每次查询的就是在转移边上得到节点的parent树中后缀节点数量.
由于强制在线,可以用动态树维护后缀自动机parent树的子树和。
注意一个玄学的优化:每次在执行连边操作时,让parent节点作为x,新节点作为y,否则在一串字符相同的串会被莫名其妙卡成N方。
压行后的lct和sam
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
struct node { int len, link, sz; map<char, int> next; } sam[1200010];
char tmp[3000010], tmp1[233];
int ch[1200010][2], fa[1200010], st[1200010], sz[1200010], s[1200010], mask, qcnt, tot, last;
bool lazy[1200010], val[1200010];
void decode() { for (int j = 0, mask1 = mask, len = strlen(tmp); j < len; j++) swap(tmp[j], tmp[mask1 = (mask1 * 131 + j) % len]); }
bool nroot(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
void rev(int x) { swap(ch[x][0], ch[x][1]), lazy[x] ^= 1; }
void pushup(int x) { sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + s[x] + val[x]; }
void pushdown(int x) { if (lazy[x]) { if (ch[x][0]) { rev(ch[x][0]); } if (ch[x][1]) { rev(ch[x][1]); } lazy[x] = 0; } }
void rotate(int x)
{
int y = fa[x], z = fa[y], k = ch[y][1] == x, w = ch[x][k ^ 1];
if (nroot(y)) { ch[z][ch[z][1] == y] = x; } ch[x][k ^ 1] = y, ch[y][k] = w;
if (w) { fa[w] = y; } fa[y] = x; fa[x] = z; pushup(y), pushup(x);
}
void splay(int x)
{
int y = x, top = 0; st[++top] = y; while (nroot(y)) { st[++top] = y = fa[y]; } while (top > 0) { pushdown(st[top--]); }
while (nroot(x)) { int y = fa[x], z = fa[y]; if (nroot(y)) { rotate((ch[y][1] == x) ^ (ch[z][1] == y) ? x : y); } rotate(x); }
}
void access(int x) { for (int y = 0; x > 0; x = fa[y = x]) splay(x), s[x] += sz[ch[x][1]] - sz[y], ch[x][1] = y, pushup(x); }
void makert(int x) { access(x), splay(x), rev(x); }
int findrt(int x) { access(x), splay(x); while (ch[x][0]) { pushdown(x), x = ch[x][0]; } return x; }
void link(int x, int y) { makert(x); if (findrt(y) != x) fa[x] = y, s[y] += sz[x], pushup(y); }
void cut(int x, int y) { makert(x); if (findrt(y) == x && fa[x] == y && ch[x][1] == 0) ch[y][0] = fa[x] = 0, pushup(y); }
void insert(char ch)
{
int cur = ++tot, p = last; sam[cur].len = sam[last].len + 1, sam[cur].sz = 1, sam[cur].next.clear(), sz[tot] = val[tot] = 1;
while (p != -1 && sam[p].next.count(ch) == false) sam[p].next[ch] = cur, p = sam[p].link;
if (p == -1) { sam[cur].link = 1, link(1, cur); }
else
{
int q = sam[p].next[ch];
if (sam[p].len + 1 == sam[q].len) { sam[cur].link = q, link(q, cur); }
else
{
int cjh = ++tot;
sam[cjh].len = sam[p].len + 1, sam[cjh].link = sam[q].link, link(sam[q].link, cjh), sam[cjh].next = sam[q].next;
while (p != -1 && sam[p].next[ch] == q) sam[p].next[ch] = cjh, p = sam[p].link;
if (sam[q].link) { cut(sam[q].link, q); } sam[q].link = sam[cur].link = cjh, link(cjh, q), link(cjh, cur);
}
}
last = cur;
}
int main()
{
tot = 1, last = 1; sam[1].len = 0, sam[1].link = -1, sam[1].sz = 1, sam[1].next.clear(), sz[tot] = val[tot] = 1;
scanf("%d%s", &qcnt, tmp); for (int j = 0; tmp[j] != 0; j++) insert(tmp[j]);
for (int i = 1; i <= qcnt; i++)
{
scanf("%s%s", tmp1, tmp), decode();
if (!strcmp(tmp1, "ADD")) for (int j = 0; tmp[j] != 0; j++) insert(tmp[j]);
else
{
int p = 1;
for (int j = 0; tmp[j] != 0; j++)
if (sam[p].next.count(tmp[j])) p = sam[p].next[tmp[j]];
else { printf("0\n"); goto escape; }
cut(p, sam[p].link), makert(p);
int ans = sz[p];
link(sam[p].link, p);
printf("%d\n", ans), mask ^= ans;
} escape:;
}
return 0;
}
luogu5212/bzoj2555 substring(后缀自动机+动态树)的更多相关文章
- BZOJ3413: 匹配(后缀自动机 线段树合并)
题意 题目链接 Sol 神仙题Orz 后缀自动机 + 线段树合并... 首先可以转化一下模型(想不到qwq):问题可以转化为统计\(B\)中每个前缀在\(A\)中出现的次数.(画一画就出来了) 然后直 ...
- cf666E. Forensic Examination(广义后缀自动机 线段树合并)
题意 题目链接 Sol 神仙题Orz 后缀自动机 + 线段树合并 首先对所有的\(t_i\)建个广义后缀自动机,这样可以得到所有子串信息. 考虑把询问离线,然后把\(S\)拿到自动机上跑,同时维护一下 ...
- 洛谷P2178 [NOI2015]品酒大会(后缀自动机 线段树)
题意 题目链接 Sol 说一个后缀自动机+线段树的无脑做法 首先建出SAM,然后对parent树进行dp,维护最大次大值,最小次小值 显然一个串能更新答案的区间是\([len_{fa_{x}} + 1 ...
- BZOJ1396: 识别子串(后缀自动机 线段树)
题意 题目链接 Sol 后缀自动机+线段树 还是考虑通过每个前缀的后缀更新答案,首先出现次数只有一次,说明只有\(right\)集合大小为\(1\)的状态能对答案产生影响 设其结束位置为\(t\),代 ...
- [Luogu5161]WD与数列(后缀数组/后缀自动机+线段树合并)
https://blog.csdn.net/WAautomaton/article/details/85057257 解法一:后缀数组 显然将原数组差分后答案就是所有不相交不相邻重复子串个数+n*(n ...
- 洛谷P4493 [HAOI2018]字串覆盖(后缀自动机+线段树+倍增)
题面 传送门 题解 字符串就硬是要和数据结构结合在一起么--\(loj\)上\(rk1\)好像码了\(10k\)的样子-- 我们设\(L=r-l+1\) 首先可以发现对于\(T\)串一定是从左到右,能 ...
- 模板—字符串—后缀自动机(后缀自动机+线段树合并求right集合)
模板—字符串—后缀自动机(后缀自动机+线段树合并求right集合) Code: #include <bits/stdc++.h> using namespace std; #define ...
- 【BZOJ4556】[TJOI2016&HEOI2016] 字符串(后缀自动机+线段树合并+二分)
点此看题面 大致题意: 给你一个字符串\(s\),每次问你一个子串\(s[a..b]\)的所有子串和\(s[c..d]\)的最长公共前缀. 二分 首先我们可以发现一个简单性质,即要求最长公共前缀,则我 ...
- bzoj3879 SvT(后缀自动机+虚树)
bzoj3879 SvT(后缀自动机+虚树) bzoj 有一个长度为n的仅包含小写字母的字符串S,下标范围为[1,n]. 现在有若干组询问,对于每一个询问,我们给出若干个后缀(以其在S中出现的起始位置 ...
随机推荐
- 「小程序JAVA实战」微信开发者工具helloworld(三)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-03/ 第一个小程序demo的运行,首选需要去使用开发工具 开发工具下载安装 https://mp. ...
- Tornado 高并发源码分析之五--- IOLoop 对象
IOLoop主要工作 1.将TCPServer 注册到 IOLoop 的事件记到 _handlers 字段,同时注册 READ 和 ERROR 事件到 epoll 2.IOLoop 启动一个大循环,负 ...
- Android中资源文件夹res/raw和assets的使用
Android中资源文件夹res/raw和assets的使用 2011-12-08 11:05 494人阅读 评论(0) 收藏 举报 androidxml存储stringencodinglayout ...
- Perl基础语法
一.脚本文件perl 代码可以写在一个文本文件中,以 .pl..PL 作为后缀.文件名可以包含数字,符号和字母,但不能包含空格,可以使用下划线(_)来替代空格.一个简单的Perl 文件名:rurun_ ...
- 关于CountDownLatch控制线程的执行顺序
在上一篇文章中说过使用thread.join()方法.newSingleThreadExecutor单线程池来控制线程执行顺序.在文章的末尾我提出了一种构想,可否使用经典的生产者和消费者模型来控制执行 ...
- rsa 数学推论
RSA加密算法是最常用的非对称加密算法,CFCA在证书服务中离不了它.但是有不少新来的同事对它不太了解,恰好看到一本书中作者用实例对它进行了简化 而生动的描述,使得高深的数学理论能够被容易地理解.我们 ...
- 1、 Shiro框架:认证,授权(验权 2. Shiro框架实现权限控制方式:
1. Shiro框架:认证,授权(验权) a) 认证逻辑:applicationCode—>通过工具类获取subject对象,调用login方法参数令牌信息->安全管理器------> ...
- Yii2邮箱发送与配置
1配置邮箱 在 common/config/web.php中写入以下代码配置 Mail代理 return [ 'components' => [ ...//your code, //以下是 ma ...
- 数据库去重与join连表
join连表删除的效率与检测存在之后删除的效率比,后者的效率低了很多
- Matrix和Camera配合实现3D效果
一.Camera与Matrix简单介绍 1.Camera Android中一共有两个Camera,分别为:android.graphics.Camera android.hardware.Camera ...