bzoj 2555 SubString

来源:互联网 发布:plc编程软件for mac 编辑:程序博客网 时间:2024/06/02 05:42

2555: SubString

Time Limit: 30 Sec  Memory Limit: 512 MB
Submit: 2611  Solved: 784
[Submit][Status][Discuss]

Description

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

Input

    第一行一个数Q表示操作个数
    
    第二行一个字符串表示初始字符串init
    
    接下来Q行,每行2个字符串Type,Str 
    
    Type是ADD的话表示在后面插入字符串。
    
    Type是QUERY的话表示询问某字符串在当前字符串中出现了几次。
    
    为了体现在线操作,你需要维护一个变量mask,初始值为0
   
    
    读入串Str之后,使用这个过程将之解码成真正询问的串TrueStr。
    询问的时候,对TrueStr询问后输出一行答案Result
    然后mask = mask xor Result  
    插入的时候,将TrueStr插到当前字符串后面即可。

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

Output

Sample Input

2

A

QUERY B

ADD BBABBBBAAB

Sample Output


0

HINT

 40 % 的数据字符串最终长度 <= 20000,询问次数<= 1000,询问总长度<= 10000

    

100 % 的数据字符串最终长度 <= 600000,询问次数<= 10000,询问总长度<= 3000000


新加数据一组--2015.05.20

   

Source

Ctsc模拟赛By 洁妹







【分析】

实诚滴讲,这道题思维难度不大...就是得码一天QAQ

首先建出来后缀自动机,搞出来parent树,每次query的答案就是串在自动机上跑到的节点的right集合的大小...然后就遇到了非常棘手的事情...这棵parent树变来变去...Orz。所以用LCT维护一下...每次cut(x)就需要把f[x]到root的路径的right集合大小减去|right[x]|,每次link就要把f[x]到root的路径的right集合大小加上|right[x]|,这个用LCT可以做到...

对了...因为parent树事实上是一只有向树...所以可以不用Make_Root操作...233

然后解码比较奇葩...大家注意一下。给的样例我也不想吐槽了。




【代码】

#include<iostream>#include<cstring>#include<cstdio>#define ll long long#define M(a) memset(a,0,sizeof a)#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;const int mxn=1200005;char s[mxn],opt[mxn];int Q,top,len,tot,root,mask;inline int read(){    int x=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}    while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();    return x*f;}struct Link_Cut_Tree{    int f[mxn],ch[mxn][2],add[mxn],st[mxn],rig[mxn];    inline bool isroot(int x) {return ch[f[x]][0]!=x && ch[f[x]][1]!=x;}    inline int get(int x) {return ch[f[x]][1]==x?1:0;}    inline void ope(int x,int c) {add[x]+=c,rig[x]+=c;}    inline void pushdown(int x)    {        if(add[x])        {            if(ch[x][0]) ope(ch[x][0],add[x]);            if(ch[x][1]) ope(ch[x][1],add[x]);            add[x]=0;        }    }    inline void rotate(int x){    pushdown(x);    int fa=f[x],fafa=f[fa],which=get(x);    if(!isroot(fa)) ch[fafa][ch[fafa][1]==fa]=x;    f[x]=fafa;    ch[fa][which]=ch[x][which^1],f[ch[fa][which]]=fa;    ch[x][which^1]=fa,f[fa]=x;}inline void splay(int x){      st[top=1]=x;      for(int i=x;!isroot(i);i=f[i]) st[++top]=f[i];      for(int i=top;i;i--) pushdown(st[i]);      for(int fa;!isroot(x);rotate(x))        if(!isroot(fa=f[x])) rotate(get(x)==get(fa)?fa:x);}inline void access(int x){    for(int y=0;x;y=x,x=f[x])      splay(x),ch[x][1]=y;}inline void link(int x,int y){    f[x]=y;    access(y),splay(y),ope(y,rig[x]);}inline void cut(int x){    access(x),splay(x);    ope(ch[x][0],-rig[x]);    f[ch[x][0]]=0,ch[x][0]=0;}}LCT;struct String{    int p,q,np,nq;    int son[mxn][30],step[mxn],pre[mxn];    void Decode(char s[],int mask){    for(int j=0;j<len;j++){        mask=(mask*131+j)%len;        swap(s[j],s[mask]);    }}    inline void add(int c)    {        p=np;        step[np=(++tot)]=step[p]+1;        LCT.rig[np]=1;        while(p && !son[p][c])          son[p][c]=np,p=pre[p];        if(!p) {pre[np]=1,LCT.link(np,pre[np]);return;}        q=son[p][c];        if(step[q]==step[p]+1) {pre[np]=q,LCT.link(np,pre[np]);return;}        step[nq=(++tot)]=step[p]+1;//LCT.rig[nq]=1;        memcpy(son[nq],son[q],sizeof son[q]);        pre[nq]=pre[q];        pre[q]=pre[np]=nq;        LCT.cut(q),LCT.link(nq,pre[nq]);LCT.link(np,pre[np]),LCT.link(q,pre[q]);        while(p && son[p][c]==q)          son[p][c]=nq,p=pre[p];    }    inline int find()    {    int i,j;p=1;    fo(i,0,len-1)    {    int c=s[i]-'A';    if(!son[p][c]) return 0;  //假的,不存在的.     p=son[p][c];}LCT.access(p),LCT.splay(p);return LCT.rig[p];}}SAM;int main(){int i,j;Q=read();scanf("%s",s+1);len=strlen(s+1);root=tot=SAM.np=1;fo(i,1,len) SAM.add(s[i]-'A');while(Q--){scanf("%s%s",opt,s);len=strlen(s);SAM.Decode(s,mask);//cout<<s[0]<<endl;if(opt[0]=='A')  fo(i,0,len-1) SAM.add(s[i]-'A');else{int tmp=SAM.find();printf("%d\n",tmp);mask^=tmp;}}return 0;}/*1AAQUERY A*/



原创粉丝点击