Bzoj2555:substring SAM+LCT

Source: Internet
Author: User

Test instructions: Lazy to write the background, give you a string init, ask you to support two operations
(1): Inserts a string after the current string
(2): How many times does the query string s appear in the current string? (as continuous substring)
You must support these operations online.
Problems: Can think of Sam very good maintenance of a string in the current string appears a few times, insert also directly add is good, but we can not every time the query topo and then update SZ, can think of whether you can add a new character each time, constantly jumping to FA, until jumping to the root, But the worst is also O (n), we consider how to quickly maintain Sam's FA tree plus edge Delete, update the chain weights, it is obvious to think of lct,lct play lazy tag update chain can, Sam in the update FA in the LCT on the addition of the edge on the good, The query is splay to the root of the current splay tree and asks Val
Note: When creating a new NQ, the SZ value of Q should be assigned to SZ[NQ]

/************************************************************** problem:2555 user:walfy language:c++ Result  : Accepted time:20820 ms memory:177688 kb****************************************************************///#pragma GCC optimize (2)//#pragma gcc optimize (3)//#pragma gcc optimize (4)//#pragma gcc optimize ("unroll-loops")//#pragma Comment (linker, "/stack:200000000")//#pragma gcc optimize ("ofast,no-stack-protector")//#pragma gcc target ("Sse,sse2 , sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ") #include <bits/stdc++.h> #define FI first#define se second# Define DB Double#define MP Make_pair#define pb push_back#define Pi ACOs ( -1.0) #define LL long Long#define VI vector<int& gt; #define MOD 1000000007#define ld long double#define C 0.5772156649#define ls l,m,rt<<1#define rs m+1,r,rt<&lt ; 1|1#define PLL pair<ll,ll> #define PIL pair<int,ll> #define PLI pair<ll,int> #define PII pair<int, int>//#define CD complex<double> #define ULL UNSigned Long long#define base 1000000000000000000#define Max (a) > (b)? ( A):(B) #define MIN (a) < (b) ( A):(B)) #define FIN freopen ("A.txt", "R", stdin) #define Fout freopen ("A.txt", "w", stdout) #define FIO Ios::sync_with_ Stdio (false); Cin.tie (0) template<typename t>inline t const& MAX (T const &a,t const &b) {return A>b?a : b;} Template<typename t>inline T const& MIN (T const &a,t const &AMP;B) {return a<b?a:b;} inline void Add (ll &a,ll b) {a+=b;if (a>=mod) A-=mod;} inline void Sub (ll &a,ll b) {a-=b;if (a<0) A+=mod;} inline ll gcd (ll A,ll b) {return B?GCD (b,a%b): A;} inline ll QP (ll A,ll b) {ll ans=1;while (b) {if (b&1) ans=ans*a%mod;a=a*a%mod,b>>=1;} return ans;} inline ll QP (ll a,ll b,ll c) {ll ans=1;while (b) {if (b&1) ans=ans*a%c;a=a*a%c,b>>=1;} return ans;} using namespace Std; Const DOUBLE EPS=1E-8;CONST ll inf=0x3f3f3f3f3f3f3f3f;const int n=600000+10,maxn=3000000+10,inf=0x3f3f3f3f; char s[maxn];struct lct{int fa[n<<1],ch[n<<1][2],rev[n<<1],sz[n<<1],q[n<<1],val[n<<1],add[n<<1];    inline bool IsRoot (int x) {return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}    inline void pushup (int x) {sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;            } inline void pushdown (int x) {if (rev[x]) {Rev[x]=0;swap (ch[x][0],ch[x][1]);        Rev[ch[x][0]]^=1,rev[ch[x][1]]^=1;            } if (Add[x]) {if (ch[x][0]) val[ch[x][0]]+=add[x],add[ch[x][0]]+=add[x];            if (ch[x][1]) val[ch[x][1]]+=add[x],add[ch[x][1]]+=add[x];        add[x]=0;        }} inline void Rotate (int x) {int y=fa[x],z=fa[y],l,r;        if (ch[y][0]==x) l=0,r=l^1;        else l=1,r=l^1;            if (!isroot (y)) {if (ch[z][0]==y) ch[z][0]=x;        else ch[z][1]=x;        } fa[x]=z;fa[y]=x;fa[ch[x][r]]=y;        Ch[y][l]=ch[x][r];ch[x][r]=y;    Pushup (y);    } inline void splay (int x) {int top=1;q[top]=x;    for (int i=x;!        IsRoot (i); I=fa[i]) q[++top]=fa[i];        for (int i=top;i;i--) pushdown (Q[i]);            while (!isroot (x)) {int y=fa[x],z=fa[y];                if (!isroot (y)) {if ((ch[y][0]==x) ^ (ch[z][0]==y)) Rotate (x);            else Rotate (y);        } Rotate (x);    } pushup (x);    } inline void access (int x) {for (int y=0;x;y=x,x=fa[x]) splay (x), Ch[x][1]=y,pushup (x);}    inline void makeroot (int x) {access (x), splay (x), rev[x]^=1;}    inline int findroot (int x) {access (x), splay (x), while (Ch[x][0]) X=ch[x][0];return x;}    inline void split (int x,int y) {makeroot (x), Access (y), splay (y);}    inline void cut (int x,int y) {split (x, Y), if (ch[y][0]==x) ch[y][0]=0,fa[x]=0;} inline void link (int x,int y) {makeroot (x), Fa[x]=y,splay (x);}}    lct;struct sam{int last,cnt;    int ch[n<<1][26],fa[n<<1],l[n<<1],sz[n<<1];        void ins (int c) {int p=last,np=++cnt;last=np;l[np]=l[p]+1; for (;p &&!ch[p][C];p =fa[p]) CH[P][C]=NP;        if (!p) Fa[np]=1,lct.link (np,1);            else {int q=ch[p][c];            if (L[p]+1==l[q]) Fa[np]=q,lct.link (NP,Q);                else {int nq=++cnt;l[nq]=l[p]+1;                memcpy (ch[nq],ch[q],sizeof (ch[q));                Lct.link (Nq,fa[q]); Lct.link (NP,NQ);                Lct.cut (Q,fa[q]); Lct.link (Q,NQ);                FA[NQ]=FA[Q];FA[Q]=FA[NP]=NQ;                Lct.splay (NQ); Lct.splay (q);                LCT.VAL[NQ]=LCT.VAL[Q];            for (; ch[p][c]==q;p=fa[p]) ch[p][c]=nq;        }} lct.split (np,1);    lct.add[1]++,lct.val[1]++;//sz[np]=1;//for (; NP;NP=FA[NP]) sz[np]++;        } void Build () {int Len=strlen (s);        Last=cnt=1;    for (int i=0;i<len;i++) ins (s[i]-' A ');        } int cal () {int Len=strlen (s);        int now=1;            for (int i=0;i<len;i++) {if (ch[now][s[i]-' a ']) now=ch[now][s[i]-' a ']; else return 0;        } lct.splay (now);    return Lct.val[now];    }}sam;void Change (int mask) {int Len=strlen (s);        for (int i=0; i<len; i++) {mask= (mask*131+i)%len;    Swap (s[mask],s[i]);    }}int Main () {int n;scanf ("%d%s", &n,s);    Sam.build ();    int mask=0;        while (n--) {char op[10];        scanf ("%s%s", op,s);        Change (mask);            if (op[0]== ' A ') {int Len=strlen (s);        for (int i=0;i<len;i++) sam.ins (s[i]-' A ');            } else {int ans=sam.cal ();            Mask^=ans;        printf ("%d\n", ans); }} return 0;} /********************23ABAABQ aaq aa abaq ABQ BQ aa AAAQ aaq AQ aaa********************/

Bzoj2555:substring SAM+LCT

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.