对字符串构建一个后缀自动机.

每次查询的就是在转移边上得到节点的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(后缀自动机+动态树)的更多相关文章

  1. BZOJ3413: 匹配(后缀自动机 线段树合并)

    题意 题目链接 Sol 神仙题Orz 后缀自动机 + 线段树合并... 首先可以转化一下模型(想不到qwq):问题可以转化为统计\(B\)中每个前缀在\(A\)中出现的次数.(画一画就出来了) 然后直 ...

  2. cf666E. Forensic Examination(广义后缀自动机 线段树合并)

    题意 题目链接 Sol 神仙题Orz 后缀自动机 + 线段树合并 首先对所有的\(t_i\)建个广义后缀自动机,这样可以得到所有子串信息. 考虑把询问离线,然后把\(S\)拿到自动机上跑,同时维护一下 ...

  3. 洛谷P2178 [NOI2015]品酒大会(后缀自动机 线段树)

    题意 题目链接 Sol 说一个后缀自动机+线段树的无脑做法 首先建出SAM,然后对parent树进行dp,维护最大次大值,最小次小值 显然一个串能更新答案的区间是\([len_{fa_{x}} + 1 ...

  4. BZOJ1396: 识别子串(后缀自动机 线段树)

    题意 题目链接 Sol 后缀自动机+线段树 还是考虑通过每个前缀的后缀更新答案,首先出现次数只有一次,说明只有\(right\)集合大小为\(1\)的状态能对答案产生影响 设其结束位置为\(t\),代 ...

  5. [Luogu5161]WD与数列(后缀数组/后缀自动机+线段树合并)

    https://blog.csdn.net/WAautomaton/article/details/85057257 解法一:后缀数组 显然将原数组差分后答案就是所有不相交不相邻重复子串个数+n*(n ...

  6. 洛谷P4493 [HAOI2018]字串覆盖(后缀自动机+线段树+倍增)

    题面 传送门 题解 字符串就硬是要和数据结构结合在一起么--\(loj\)上\(rk1\)好像码了\(10k\)的样子-- 我们设\(L=r-l+1\) 首先可以发现对于\(T\)串一定是从左到右,能 ...

  7. 模板—字符串—后缀自动机(后缀自动机+线段树合并求right集合)

    模板—字符串—后缀自动机(后缀自动机+线段树合并求right集合) Code: #include <bits/stdc++.h> using namespace std; #define ...

  8. 【BZOJ4556】[TJOI2016&HEOI2016] 字符串(后缀自动机+线段树合并+二分)

    点此看题面 大致题意: 给你一个字符串\(s\),每次问你一个子串\(s[a..b]\)的所有子串和\(s[c..d]\)的最长公共前缀. 二分 首先我们可以发现一个简单性质,即要求最长公共前缀,则我 ...

  9. bzoj3879 SvT(后缀自动机+虚树)

    bzoj3879 SvT(后缀自动机+虚树) bzoj 有一个长度为n的仅包含小写字母的字符串S,下标范围为[1,n]. 现在有若干组询问,对于每一个询问,我们给出若干个后缀(以其在S中出现的起始位置 ...

随机推荐

  1. JavaScript第二节

    1.动态属性 引用类型可以动态的添加属性,而基本类型不行. 2.复制变量值 3.检测类型 执行环境和作用域 没有块级作用域 引用类型 1.Object类型 2.Array类型 数组初始化: 检测数组: ...

  2. BurpSuite—-Spider模块(蜘蛛爬行)

    一.简介 Burp Spider 是一个映射 web 应用程序的工具.它使用多种智能技术对一个应用程序的内容和功能进行全面的清查. Burp Spider 通过跟踪 HTML 和 JavaScript ...

  3. Python基础学习四 函数

    1.内置函数 Python内置了很多有用的函数,是可以直接调用的. 参考链接:https://docs.python.org/3/library/functions.html 调用函数的时候,如果传入 ...

  4. bash&nbsp;shell笔记3&nbsp;结构化命令二

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://twentyfour.blog.51cto.com/945260/513601 三 ...

  5. java通过反射了解集合泛型的本质

  6. 前端html数组去重的方法

    数组去重 用到的知识点: 1:indexOf() 该方法是返回数组中元素第一次出现的索引值: 如果有,则正常返回索引值: 如果检索的内容不存在于数组中,则返回-1 2:for循环 练习:数组去重 // ...

  7. 读书笔记 Week5 2018-4-5

    再结束了第一个个人任务以后,我也算有点时间翻开一本大部头来通读一下.在看了一些相关的评论说:“该书可以从任意章节读起”后,刚刚在180M测试文件的个人任务中吃了亏的我,决定从他的第5部分,代码改善看起 ...

  8. 【HDU4966】GGS-DDU

    题意 有n种科目,每个科目都有一个最高的等级a[i].开始的时候,每个科目的等级都是0.现在要选择一些课程进行学习使得每一个科目都达到最高等级.这里有m节课可供选择.对于每门课给出L1[i],c[i] ...

  9. 【COCI2012】覆盖字符串

    [题目描述] 给出一个长度为N的小写字母串,现在Mirko有M个若干长度为Li字符串.现在Mirko要用这M个字符串去覆盖给出的那个字符串的.覆盖时,必须保证:1.Mirko的字符串不能拆开,旋转:2 ...

  10. laravel Redis缓存

    首先在app/config/cache.php配置文件下改变一下缓存的驱动方式改为redis composer require predis/predis 先安装conposer的扩展安装包 然后在c ...