$ \color{#0066ff}{ 题目描述 }$

给定一个字符串init,要求支持两个操作

  • 在当前字符串的后面插入一个字符串
  • 询问字符串ss在当前字符串中出现了几次?(作为连续子串)

强制在线。

\(\color{#0066ff}{输入格式}\)

第一行一个整数\(Q\)表示操作个数

第二行一个字符串表示初始字符串init

接下来Q行,每行2个字符串Type,Str

  • TypeADD,表示在后面插入字符串。
  • TypeQUERY,表示询问某字符串在当前字符串中出现了几次。

为了体现在线操作,你需要维护一个变量mask,初始值为00

读入串Str之后,使用这个过程将之解码成真正询问的串TrueStr

询问的时候,对TrueStr询问后输出一行答案Result

然后\(mask=mask \bigoplus Result\)

插入的时候,将TrueStr插到当前字符串后面即可。

注意:ADD和QUERY操作的字符串都需要解压

\(\color{#0066ff}{输出格式}\)

对于每一个QUERY操作,输出询问的字符串在当前字符串中出现了几次。

\(\color{#0066ff}{输入样例}\)

2
A
QUERY B
ADD BBABBBBAAB

\(\color{#0066ff}{输出样例}\)

0

\(\color{#0066ff}{数据范围与提示}\)

\(∣S∣≤6×10^5,Q \leq 10^4\),询问总长度\(\leq 3 \times 10^6\)

为防止评测过慢,对于测试点2 3 5 6 8 11 时限为3s,其余为1s

\(\color{#0066ff}{题解}\)

每次插入字符,还要匹配, 显然SAM再合适不过

匹配的时候,找到那个点\(O(len)\),那么答案就是parent树的子树大小

但是这个树是动态的。。,于是。。。。LCT啊。。

LCT维护子树和即可

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define LL long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 6e6 + 1;
const int maxm = 1.2e6 + 1;
struct LCT {
protected:
struct node {
node *ch[2], *fa;
int tot, siz, val, rev;
node(int tot = 0, int siz = 0, int val = 0, int rev = 0): tot(tot), siz(siz), val(val), rev(rev) {}
void trn() { std::swap(ch[0], ch[1]), rev ^= 1; }
void dwn() {
if(!rev) return;
if(ch[0]) ch[0]->trn();
if(ch[1]) ch[1]->trn();
rev = 0;
}
void upd() {
tot = siz + val;
if(ch[0]) tot += ch[0]->tot;
if(ch[1]) tot += ch[1]->tot;
}
bool isr() { return fa->ch[1] == this; }
bool ntr() { return fa && (fa->ch[1] == this || fa->ch[0] == this); }
}pool[maxm];
void rot(node *x) {
node *y = x->fa, *z = y->fa;
bool k = x->isr(); node *w = x->ch[!k];
if(y->ntr()) z->ch[y->isr()] = x;
(x->ch[!k] = y)->ch[k] = w;
(y->fa = x)->fa = z;
if(w) w->fa = y;
y->upd(), x->upd();
}
void splay(node *o) {
static node *st[maxm];
int top;
st[top = 1] = o;
while(st[top]->ntr()) st[top + 1] = st[top]->fa, top++;
while(top) st[top--]->dwn();
while(o->ntr()) {
if(o->fa->ntr()) rot(o->isr() ^ o->fa->isr()? o : o->fa);
rot(o);
}
}
void access(node *x) {
for(node *y = NULL; x; x = (y = x)->fa) {
splay(x);
if(x->ch[1]) x->siz += x->ch[1]->tot;
if((x->ch[1] = y)) x->siz -= x->ch[1]->tot;
x->upd();
}
}
void makeroot(node *x) { access(x), splay(x), x->trn(); }
void link(node *x, node *y) {
makeroot(x), access(y), splay(y);
(x->fa = y)->siz += x->tot;
y->upd();
}
void cut(node *x, node *y) {
makeroot(y), access(x), splay(x);
assert(x->ch[0] == y);
x->ch[0] = y->fa = NULL, x->upd();
}
public:
friend struct SAM;
}c;
struct SAM {
protected:
struct node {
node *ch[26], *fa;
int len, siz;
node(int len = 0, int siz = 0): len(len), siz(siz) {}
}pool[maxm];
node *root, *tail, *lst;
LCT::node *id(node *x) { return c.pool + (x - pool); }
void extend(int s) {
node *o = new(tail++) node(lst->len + 1, 1), *v = lst;
id(o)->val = 1, id(o)->upd();
for(; v && !v->ch[s]; v = v->fa) v->ch[s] = o;
if(!v) o->fa = root, c.link(id(o), id(root));
else if(v->len + 1 == v->ch[s]->len) o->fa = v->ch[s], c.link(id(o), id(v->ch[s]));
else {
node *n = new(tail++) node(v->len + 1), *d = v->ch[s];
std::copy(d->ch, d->ch + 26, n->ch);
id(n)->upd();
c.cut(id(d), id(d->fa));
c.link(id(n), id(d->fa));
c.link(id(d), id(n));
c.link(id(o), id(n));
n->fa = d->fa, d->fa = o->fa = n;
for(; v && v->ch[s] == d; v = v->fa) v->ch[s] = n;
}
lst = o;
}
void clr() {
tail = pool;
root = lst = new(tail++) node();
}
public:
SAM() { clr(); }
void ins(char *s) { for(char *p = s; *p; p++) extend(*p - 'A'); }
int getans(char *s) {
node *o = root;
for(char *p = s; *p; p++) {
int pos = *p - 'A';
if(o->ch[pos]) o = o->ch[pos];
else return 0;
}
c.makeroot(id(root));
c.access(id(o));
c.splay(id(o));
return id(o)->val + id(o)->siz;
}
}s;
char ls[maxn];
int n, len;
void doit(int ans) {
for(int i = 0; i < len; i++) {
ans = (ans * 131 + i) % len;
std::swap(ls[i], ls[ans]);
}
}
int main() {
n = in();
scanf("%s", ls);
s.ins(ls);
int mask = 0, ans;
while(n --> 0) {
scanf("%s", ls);
if(ls[0] == 'A') {
scanf("%s", ls);
len = strlen(ls);
doit(mask);
s.ins(ls);
}
else {
scanf("%s", ls);
len = strlen(ls);
doit(mask);
printf("%d\n", ans = s.getans(ls));
mask ^= ans;
}
}
return 0;
}

P5212 SubString LCT+SAM的更多相关文章

  1. 2555: SubString[LCT+SAM]

    2555: SubString Time Limit: 30 Sec  Memory Limit: 512 MB Submit: 2601  Solved: 780 [Submit][Status][ ...

  2. 【BZOJ 2555】 2555: SubString (SAM+LCT)

    2555: SubString Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 2548  Solved: 762 Description 懒得写背景了 ...

  3. bzoj 2555 SubString(SAM+LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2555 [题意] 给定一个字符串,可以随时插入字符串,提供查询s在其中作为连续子串的出现 ...

  4. 2019.03.01 bzoj2555: SubString(sam+lct)

    传送门 题意简述: 要求在线支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 思路: 考虑用lctlctlct来动态维护samsa ...

  5. [BZOJ2555]SubString LCT+后缀自动机

    2555: SubString Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 3253  Solved: 975[Submit][Status][Di ...

  6. BZOJ2555 SubString【SAM + Link Cut Tree】

    BZOJ2555. SubString 要求在线询问一个串在原串中出现的次数,并且可以在原串末尾添加字符串 如果没有修改的话,考虑建出\(parent\)树之后统计每个\(endpos\)节点的\(r ...

  7. P5212-SubString【LCT,SAM】

    正题 题目链接:https://www.luogu.com.cn/problem/P5212 题目大意 开始一个字符串\(S\),有\(n\)次操作 在\(S\)末尾加入一个字符串 询问一个串在\(S ...

  8. LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)

    A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...

  9. LOJ #6041. 「雅礼集训 2017 Day7」事情的相似度 LCT+SAM+线段树

    Code: #include<bits/stdc++.h> #define maxn 200003 using namespace std; void setIO(string s) { ...

随机推荐

  1. 【转】Rails 4中使用 Bootstrap 3

    转自:http://rvg.me/2013/11/using-bootstrap-3-with-rails-4/ If you are looking to use Bootstrap 3 with ...

  2. Winsock 传输文件

    文件传输的原理:发送方把文件读到socket发送端缓冲区中,接收方把socket接收端缓端冲区中的数据写到一个新文件中.当然了,大文件需要循环读写! 服务器端为发送端: #include " ...

  3. Solaris11修改主机名

    在Solaris10中,主机名的修改是通过修改相关的配置文件实现的.在Solaris11中,主机名的配置信息已经转移到SMF配置库中,因此修改主机名的方式与Solaris10完全不同.以下是修改Sol ...

  4. javascript删除option选项的多种方法总结

    转自:https://blog.csdn.net/xiaoxuanyunmeng/article/details/16886505 1. JavaScript 代码如下: var oSel=docum ...

  5. Microsoft Office Visio 2010如何创建UML 用例图

    转自:https://blog.csdn.net/mmoooodd/article/details/10513059 1..在Microsoft Office2010中打开Microsoft Visi ...

  6. LAMP 3.5 mysql备份与恢复

    备份库的命令 mysqldump -uroot -pwangshaojun discuz > /data/discuz.sql 指定用户密码,重定向到某文件 恢复 mysql -uroot -p ...

  7. LAMP 2.4 Apache访问控制

    通过查看日志发现有个IP 恶意攻击你的网址,可以控制这个IP的访问. 打开主配置文件复制模板. vim /usr/local/apache2/conf/httpd.conf 搜索 /Order 复制 ...

  8. 关于大数据领域各个组件打包部署到集群运行的总结(含手动和maven)(博主推荐)

    对于这里的打包,总结: (1)     最简单的,也是为了适应公司里,还是要用maven,当然spark那边sbt,maven都可以.但是maven居多. Eclipse/MyEclipse下如何Ma ...

  9. Linux之Ubuntu环境配置(一)

    Linux下的搜狗输入法安装: 1.搜狗官网下载Linux64bit版本文件,默认在/home/username/Downloads目录下. 2.cd /home/username/Downloads ...

  10. 《Android应用性能优化》 第8章 图形

    1.例子中 30个部件的xml setContentView 几乎占用了从onCreate() 到 onResume() 结束之前所有时间的99% 因为展开布局的开销很大.要尽量用不同的布局方式.比如 ...