询问串放在SAM上不跳fail跑到的节点的|right|即为答案。用LCT维护parent树即可。可以直接维护子树信息,也可以转化为路径加。注意强制在线所使用的mask是作为参数传进去的。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 1300010
#define lson tree[k].ch[0]
#define rson tree[k].ch[1]
#define lself tree[tree[k].fa].ch[0]
#define rself tree[tree[k].fa].ch[1]
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
int x=0,f=1;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
int Q,n,m,cnt=1,last=1,son[N][26],fail[N],len[N],mask;
char s[N],t[N];
struct data{int ch[2],fa,rev,s,sv,x;
}tree[N];
void up(int k){tree[k].s=tree[k].sv+tree[lson].s+tree[rson].s+tree[k].x;}
void rev(int k){if (k) swap(lson,rson),tree[k].rev^=1;}
void down(int k){if (tree[k].rev) rev(lson),rev(rson),tree[k].rev=0;}
int whichson(int k){return rself==k;}
bool isroot(int k){return lself!=k&&rself!=k;}
void push(int k){if (!isroot(k)) push(tree[k].fa);down(k);}
void move(int k)
{
int fa=tree[k].fa,gf=tree[fa].fa,p=whichson(k);
if (!isroot(fa)) tree[gf].ch[whichson(fa)]=k;tree[k].fa=gf;
tree[fa].ch[p]=tree[k].ch[!p],tree[tree[k].ch[!p]].fa=fa;
tree[k].ch[!p]=fa,tree[fa].fa=k;
up(fa),up(k);
}
void splay(int k)
{
push(k);
while (!isroot(k))
{
int fa=tree[k].fa;
if (!isroot(fa))
if (whichson(fa)^whichson(k)) move(k);
else move(fa);
move(k);
}
}
void access(int k)
{
for (int t=0;k;t=k,k=tree[k].fa)
{
splay(k);
tree[k].sv+=tree[rson].s-tree[t].s;
rson=t;
up(k);
}
}
void makeroot(int k){access(k),splay(k),rev(k);}
void link(int x,int y){makeroot(x);access(y),splay(y);tree[x].fa=y;tree[y].sv+=tree[x].s;up(y);}
void cut(int x,int y){makeroot(x),access(y),splay(y);tree[y].ch[0]=tree[x].fa=0;up(y);}
int query(int k){if (!k) return 0;makeroot(1);access(k),splay(k);return tree[k].s-tree[lson].s;}
void decode(int mask)
{
for (int j=0;j<m;j++)
{
mask=(mask*131+j)%m;
char u=t[j];t[j]=t[mask];t[mask]=u;
}
}
void relink(int x,int y)
{
if (fail[x]) cut(x,fail[x]);
fail[x]=y;link(x,y);
}
void ins(int c)
{
int x=++cnt,p=last;len[x]=len[p]+1;last=x;tree[x].x=tree[x].s=1;
while (!son[p][c]&&p) son[p][c]=x,p=fail[p];
if (!p) relink(x,1);
else
{
int q=son[p][c];
if (len[p]+1==len[q]) relink(x,q);
else
{
int y=++cnt;
len[y]=len[p]+1;
memcpy(son[y],son[q],sizeof(son[q]));
relink(y,fail[q]);
relink(q,y);
relink(x,y);
while (son[p][c]==q) son[p][c]=y,p=fail[p];
}
}
}
int run(char *s)
{
int k=1;
for (int i=0;i<m;i++) k=son[k][s[i]-'A'];
return k;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
Q=read();scanf("%s",s);n=strlen(s);
for (int i=0;i<n;i++) ins(s[i]-'A');
while (Q--)
{
scanf("%s",t);
if (t[0]=='A')
{
scanf("%s",t);m=strlen(t);
decode(mask);
for (int i=0;i<m;i++) ins(t[i]-'A');
}
else
{
scanf("%s",t);m=strlen(t);
decode(mask);
int ans=query(run(t));
printf("%d\n",ans);
mask^=ans;
}
}
return 0;
}

  

BZOJ2555 SubString(后缀自动机+LCT)的更多相关文章

  1. luogu5212/bzoj2555 substring(后缀自动机+动态树)

    对字符串构建一个后缀自动机. 每次查询的就是在转移边上得到节点的parent树中后缀节点数量. 由于强制在线,可以用动态树维护后缀自动机parent树的子树和. 注意一个玄学的优化:每次在执行连边操作 ...

  2. bzoj 2555 SubString —— 后缀自动机+LCT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2555 建立后缀自动机,就可以直接加入新串了: 出现次数就是 Right 集合的大小,需要查询 ...

  3. 【BZOJ2555】SubString 后缀自动机+LCT

    [BZOJ2555]SubString Description 懒得写背景了,给你一个字符串init,要求你支持两个操作         (1):在当前字符串的后面插入一个字符串         (2 ...

  4. bzoj 2555: SubString 后缀自动机+LCT

    2555: SubString Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 688  Solved: 235[Submit][Status][Dis ...

  5. bzoj 2555 SubString——后缀自动机+LCT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2555 要维护 right 集合的大小.因为 fa 会变,且 fa 构成一棵树,所以考虑用 L ...

  6. BZOJ2555 SubString 【后缀自动机 + LCT】

    题目 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支持这些操作. 输入 ...

  7. bzoj2555(后缀自动机+LCT)

    题目描述 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支持这些操作. 题解 做法很自然,建出后缀自动机,维护每个节点的right ...

  8. bzoj 2555: SubString【后缀自动机+LCT】

    一直WA--找了半天错的发现居然是解密那里的mask其实是不能动的--传进去的会变,但是真实的那个不会变-- 然后就是后缀自动机,用LCT维护parent树了--注意不能makeroot,因为自动机的 ...

  9. SPOJ1811 LCS - Longest Common Substring(后缀自动机)

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

  10. 51nod 1600 Simple KMP【后缀自动机+LCT】【思维好题】*

    Description 对于一个字符串|S|,我们定义fail[i],表示最大的x使得S[1..x]=S[i-x+1..i],满足(x<i) 显然对于一个字符串,如果我们将每个0<=i&l ...

随机推荐

  1. Wireshark过滤命令总结

    1.表达式1 (tcp.flags.reset == )&&(tcp.seq == ) 2.表达式2 (tcp.flags.syn == )&&(tcp.analysi ...

  2. Linux fdisk命令创建扩展分区过程

    [root@localhost ~]# fdisk /dev/sdb …省略部分输出… Command (m for help): n #新建立分区 Command action e extended ...

  3. IIS/VS IIS Express 添加MIME映射 svg、woff、woff2、json

    出现问题 页面提示 font-awesome/fonts/fontawesome-webfont.woff2?v=4.3.0 Failed to load resource: the server r ...

  4. 接口操作XML

    接口操作XML 以下代码旨在 脱离TXMLDocument 操作 xml. unit Unit3; interface uses Windows, Messages, SysUtils, Varian ...

  5. ASP.NET MVC传递Model到视图的多种方式之通用方式的使用

    ASP.NET MVC传递Model到视图的多种方式总结——通用方式的使用 有多种方式可以将数据传递到视图,如下所示: ViewData ViewBag PartialView TempData Vi ...

  6. linux物理内存与虚拟内存

    http://www.360doc.com/content/14/0123/14/14450281_347336709.shtml 1.查看内存占用情况 $ free -m -h total used ...

  7. osg::NodeVisitor example

    [0]osg::Group [1]osg::MatrixTransform [1] osg::MatrixTransform [1]osg::MatrixTransform [2] osg::Geod ...

  8. 阶段5 3.微服务项目【学成在线】_day17 用户认证 Zuul_11-前端显示当前用户-前端请求jwt

    前端代码 sessionStorage也是key/value的格式 页头显示当前用户 查询jwt nginx里面的配置 测试 开启认证服务 进行登陆 跳转到首页就应该立即请求查询. 跳转到首页 coo ...

  9. 怎样修复社区项目Karbor的Bug?

    1.准备工作. 点击Settings按钮进行设置,跳转到 https://review.openstack.org/#/settings/ 2.选择用户名称. 这个选择之后不能修改. 3.设置http ...

  10. Ubuntu 14.04 apache安装配置

    http://jingyan.baidu.com/article/6d704a130c8a0d28da51ca5f.html Ubuntu 14.04 apache安装配置 1.安装 ~# apt-g ...