【BZOJ 2555】SubString
http://www.lydsy.com/JudgeOnline/problem.php?id=2555
一个字符串在原串中的出现次数就是这个字符串对应后缀自动机上的状态的\(|Right|\),要求parent树中这个状态的子树中叶子节点的个数。
因为强制在线,所以用lct维护parent树以及树上每个节点的\(|Right|\)。
注意新建\(nq\)节点时nq节点的值要设为0,因为\(nq\)节点不是叶子节点。。。
还有删整棵子树时要减去它们对它们祖先的影响。
lct模板打错,调了好长时间_(:з」∠)_
压常数?inline大法好
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node *null;
struct node {
node *ch[2], *fa;
int r, lazy;
node() {
ch[0] = ch[1] = fa = null;
r = lazy = 0;
}
bool pl() {return fa->ch[1] == this;}
bool check() {return fa == null || (fa->ch[0] != this && fa->ch[1] != this);}
void setc(node *r, bool c) {ch[c] = r; r->fa = this;}
void push() {
if (lazy != 0) {
if (ch[0] != null) ch[0]->lazy += lazy, ch[0]->r += lazy;
if (ch[1] != null) ch[1]->lazy += lazy, ch[1]->r += lazy;
lazy = 0;
}
}
};
namespace LCT {
inline void rotate(node *r) {
node *f = r->fa;
bool c = r->pl();
if (f->check()) r->fa = f->fa;
else f->fa->setc(r, f->pl());
f->setc(r->ch[!c], c);
r->setc(f, !c);
}
inline void update(node *r) {
if (!r->check())
update(r->fa);
r->push();
}
inline void splay(node *r) {
update(r);
for(; !r->check(); rotate(r))
if (!r->fa->check()) rotate(r->pl() == r->fa->pl() ? r->fa : r);
}
inline void access(node *r) {
node *y = null;
while (r != null) {
splay(r);
r->ch[1] = y;
y = r;
r = r->fa;
}
}
inline void cut_to(node *r, node *f) {
access(r); splay(r);
int tot = r->r;
node *t = r->ch[0]; t->fa = null;
t->r -= tot; t->lazy -= tot;
r->ch[0] = null;
r->fa = f;
access(f); splay(f);
f->r += tot; f->lazy += tot;
}
inline void link_to(node *r, node *f) {
r->fa = f;
access(r); splay(r);
r->r = r->lazy = 1;
}
inline void link_to_nq(node *r, node *f) {
r->fa = f;
}
}
struct State {
State *par, *go[26];
int val;
node *to;
State(int _val) : val(_val), par(0), to(0) {
to = new node;
memset(go, 0, sizeof(go));
}
} *root, *last;
inline void extend(int w) {
State *p = last;
State *np = new State(p->val + 1);
while (p && p->go[w] == 0)
p->go[w] = np, p = p->par;
if (p == 0) {
np->par = root;
LCT::link_to(np->to, root->to);
} else {
State *q = p->go[w];
if (q->val == p->val + 1) {
np->par = q;
LCT::link_to(np->to, q->to);
} else {
State *nq = new State(p->val + 1);
memcpy(nq->go, q->go, sizeof(q->go));
nq->par = q->par;
LCT::link_to_nq(nq->to, q->par->to);
q->par = np->par = nq;
LCT::cut_to(q->to, nq->to);
LCT::link_to(np->to, nq->to);
while (p && p->go[w] == q)
p->go[w] = nq, p = p->par;
}
}
last = np;
}
inline void fix(char *s, int la) {
int len = strlen(s);
for(int i = 0; i < len; ++i) {
la = (la * 131 + i) % len;
swap(s[i], s[la]);
}
}
inline int find(char *s) {
State *tmp = root;
int len = strlen(s);
for(int i = 0; i < len; ++i)
if (tmp->go[s[i] - 'A']) tmp = tmp->go[s[i] - 'A'];
else return 0;
LCT::access(tmp->to);
LCT::splay(tmp->to);
return tmp->to->r;
}
int q, la;
char s[3000003], w[103];
int main() {
null = new node; *null = node();
root = last = new State(0);
root->to = new node;
scanf("%d", &q);
scanf("%s", s + 1);
int len = strlen(s + 1), ans;
for(int i = 1; i <= len; ++i)
extend(s[i] - 'A');
while (q--) {
scanf("%s%s", w, s);
len = strlen(s);
fix(s, la);
if (w[0] == 'Q') {
ans = find(s);
printf("%d\n", ans);
la ^= ans;
} else {
for(int i = 0; i < len; ++i)
extend(s[i] - 'A');
}
}
return 0;
}
【BZOJ 2555】SubString的更多相关文章
- 【BZOJ 2555】 2555: SubString (SAM+LCT)
2555: SubString Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 2548 Solved: 762 Description 懒得写背景了 ...
- 【BZOJ 1150】 1150: [CTSC2007]数据备份Backup (贪心+优先队列+双向链表)
1150: [CTSC2007]数据备份Backup Description 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味 的,因此你想设 ...
- Kruskal算法及其类似原理的应用——【BZOJ 3654】tree&&【BZOJ 3624】[Apio2008]免费道路
首先让我们来介绍Krukal算法,他是一种用来求解最小生成树问题的算法,首先把边按边权排序,然后贪心得从最小开始往大里取,只要那个边的两端点暂时还没有在一个联通块里,我们就把他相连,只要这个图里存在最 ...
- 【BZOJ 2957】楼房重建&&Codechef COT5 Count on a Treap&&【NOIP模拟赛】Weed 线段树的分治维护
线段树是一种作用于静态区间上的数据结构,可以高效查询连续区间和单点,类似于一种静态的分治.他最迷人的地方在于“lazy标记”,对于lazy标记一般随我们从父区间进入子区间而下传,最终给到叶子节点,但还 ...
- LCA 【bzoj 4281】 [ONTAK2015]Związek Harcerstwa Bajtockiego
[bzoj 4281] [ONTAK2015]Związek Harcerstwa Bajtockiego Description 给定一棵有n个点的无根树,相邻的点之间的距离为1,一开始你位于m点. ...
- 【BZOJ 1191】 [Apio2010]特别行动队 (斜率优化)
dsy1911: [Apio2010]特别行动队 [题目描述] 有n个数,分成连续的若干段,每段的分数为a*x^2+b*x+c(a,b,c是给出的常数),其中x为该段的各个数的和.求如何分才能使得各个 ...
- 【BZOJ 1096】 [ZJOI2007]仓库建设 (斜率优化)
1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3940 Solved: 1736 Description ...
- 【BZOJ 2132】圈地计划 && 【7.22Test】计划
两种版本的题面 Description 最近房地产商GDOI(Group of Dumbbells Or Idiots)从NOI(Nuts Old Idiots)手中得到了一块开发土地.据了解,这块土 ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
随机推荐
- 深入理解FTP协议
文件传输协议FTP(File Transfer Protocol)是因特网中使用最广泛的文件传输协议.FTP使用交互式的访问,允许客户指定文件的类型和格式(如指明是否使用ASCII码),并允许文件具有 ...
- Hash MD5 CRC 知识
本文旨在科普安全相关的知识,并附一个C#实现的文件管理工具. Hash 安全散列算法(英语:Secure Hash Algorithm,缩写为SHA)是一个密码散列函数家族,是FIPS所认证的五种安全 ...
- ADO.Net 增、删、改、查(基本项)
数据访问 对应命名空间:System.Data.SqlClient; SqlConnection:连接对象 SqlCommand:命令对象 SqlDataReader:读取器对象 CommandTex ...
- 一个简单的socket程序运行与抓包查看
为了熟悉socket编程,在ubuntu下运行了一个现有例子(Networking and Socket programming tutorial in C - CodeProject),并通过抓包查 ...
- HTML 学习笔记 JavaScript(数据类型)
字符串 数字 布尔 数组 对象 Null Undefined JavaScript 拥有动态类型 JavaScript拥有动态类型 这意味着相同的变量可用作不同的类型: 实例: var x // x ...
- Android 中布局设置导致的TextView不显示的问题
将TextView放入TableLayout中,设置TextView的Layout Witdh/Layout Height 为Wrap Content或其他大小,导致TextView内容无法显示,改为 ...
- 阿里云Center OS 6.2 Nginx 配置 SSL/TLS HTTPS配置
1.通过https://www.startssl.com/ 免费SSL证书 STARTSSL 跟VeriSign一样,StartSSL(网址:http://www.startssl.com,公司名:S ...
- Linux shell循环
条件测试 格式 test condition 或 [ condition ] 使用方括号时,要注意在条件两边加上空格,如果有操作符,运算符之间也必须有空格 测试状态:测试的结果可以用$?的值来判断,0 ...
- C语言 malloc()与sizeof运算的盲点
//malloc()与sizeof运算的盲点 #include <stdio.h> #include <stdlib.h> #include <string.h> ...
- Hadoop: MapReduce2的几个基本示例
1) WordCount 这个就不多说了,满大街都是,网上有几篇对WordCount的详细分析 http://www.sxt.cn/u/235/blog/5809 http://www.cnblogs ...