bzoj2555 LCT maintenance suffix automatic machine

Source: Internet
Author: User

The online operation of the suffix automaton is realized by maintaining the parent tree with LCT.

Note that the right value is initialized to 0, and then add a new node as long as the right value of the NP is set to 1, without changing the NQ value, because NQ is the internal node, NP is the outer node.

The idea is very simple, the code is really long, the tune for a long time .... But it's fairly clear ...

#include <bits/stdc++.h>#defineREP (I,A,B) for (int i=a;i<=b;i++)#defineMS0 (a) memset (A,0,sizeof (a))using namespaceStd;typedefLong Longll;Const intmaxn=3200100;Const intinf=1e9+Ten;CharS[MAXN];intLen;Charop[ -];intQ;structlct{intpre[maxn],ch[maxn][2],REV[MAXN]; intVAL[MAXN],ADD[MAXN]; voidinit () {MS0 (pre); MS0 (CH); MS0 (rev.);        MS0 (add);    MS0 (Val); }    voidUpdate_add (intXintW) {if(!x)return; VAL[X]+=W; ADD[X]+=W; }    voidUpdate_rev (intx) {if(!x)return; Swap (ch[x][0],ch[x][1]); REV[X]^=1; }    voidDownintx) {if(Add[x]) {Update_add (ch[x][0],add[x]); Update_add (ch[x][1],add[x]); ADD[X]=0; }        if(Rev[x]) {Update_rev (ch[x][0]); Update_rev (ch[x][1]); REV[X]=0; }    }    BOOLIsRootintx) {returnch[pre[x]][0]!=x&&ch[pre[x]][1]!=Y; }    voidUpintx) {}voidPintx) {if(!isroot (x))        P (Pre[x]);    Down (x); }    voidRotintXintkind) {        inty=Pre[x]; Ch[y][kind^1]=Ch[x][kind]; Pre[ch[x][kind]]=y; if(!isroot (y)) ch[pre[y]][ch[pre[y]][1]==y]=x; PRE[X]=Pre[y]; Ch[x][kind]=y; Pre[y]=x;    Up (y); }    voidSplay (intx) {P (x);  while(!isroot (x)) {            if(IsRoot (pre[x])) Rot (x,ch[pre[x]][0]==x); Else{                inty=pre[x],z=Pre[y]; intkind=ch[y][0]==x,one=0; if(ch[y][0]==x&&ch[z][0]==y) one=1; if(ch[y][1]==x&&ch[z][1]==y) one=1; if(one) rot (y,kind), rot (x,kind); ElseRot (X,kind), Rot (x,kind^1);    }} up (x); }    intAccessintx) {intt=0;  while(x) {splay (x); ch[x][1]=t;t=x;x=Pre[x];        Up (t); }        returnT; }    voidMakeroot (intx) {access (x); splay (x); Update_rev (x); }    voidADD (intXintYintW) {makeroot (x); Access (y); splay (y);    Update_add (Y,W); }    voidCutintXinty) {makeroot (x); Access (y); splay (y); ch[y][0]=pre[x]=0; up (y); ADD (Y,1,-val[x]); }    voidLinkintXinty) {makeroot (x);p re[x]=y;up (y); ADD (Y,1, val[x]); }    intQueryintx) {splay (x); returnVal[x]; }}; LCT LCT;structsam{intch[maxn][ -]; intPRE[MAXN],STEP[MAXN]; intLast,tot; intNewNodeintk) {++tot; Step[tot]=K;        MS0 (Ch[tot]); Pre[tot]=0; returntot; }    voidinit () {tot=0; Last=newnode (0);    Lct.init (); }    voidAddintc) {c-='A'; intP=last,np=newnode (step[p]+1); LCT.VAL[NP]=1;  while(p&&ch[p][c]==0) ch[p][c]=np,p=Pre[p]; if(p==0) pre[np]=1, Lct.link (NP,1); Else{            intq=Ch[p][c]; if(step[q]!=step[p]+1){                intNq=newnode (step[p]+1); memcpy (Ch[nq],ch[q],sizeof(Ch[q]));                Lct.cut (Q,pre[q]);                Lct.link (Nq,pre[q]);                Lct.link (Q,NQ);                Lct.link (NP,NQ); PRE[NQ]=Pre[q]; PRE[Q]=pre[np]=NQ;  while(p&&ch[p][c]==q) ch[p][c]=nq,p=Pre[p]; }            Elsepre[np]=Q,lct.link (NP,Q); } Last=NP; }    intFindChar*s) {intlen=strlen (s); intu=1; REP (i,0, len-1){            intc=s[i]-'A'; if(Ch[u][c]) u=Ch[u][c]; Else return 0; }        returnlct.query (U); }}; Sam Sam;voidDecodeChar*s,intmask) {    intlen=strlen (s); REP (i,0, len-1) {Mask= (mask*131+i)%Len;    Swap (S[i],s[mask]); }}intMain () {Freopen ("In.txt","R", stdin);  while(~SCANF ("%d",&Q)) {scanf ("%s", s); Len=strlen (s);        Sam.init (); REP (i,0, len-1) Sam.add (S[i]); intmask=0; REP (i,1, Q) {scanf ("%s%s", Op,s);            Decode (S,mask); if(op[0]=='A') {len=strlen (s); REP (i,0, len-1) Sam.add (S[i]); }            Else{                intres=Sam.find (s); printf ("%d\n", RES); Mask^=Res; }        }    }    return 0;}
View Code

bzoj2555 LCT maintenance suffix automatic machine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.