String Part 1

Source: Internet
Author: User
Tags first string

What are the string algorithms??? Tire,km,kmp,ac automaton, suffix array, suffix automaton, rk,shift-and/or,manacher .....

™ SO much AH!!!

Also can only slowly learn ...

The next question is the order I do the problem, the difficulty I will ignore (' @@facesymbol@@´)

Bzoj 2434: [NOI2011] Ali's typewriter

The first string is a man of the word!

First build the Actree and Failtree, save the stub point position of each string.

And then from the root of the tree, the roots to the current node of the string is the current typewriter string, while walking off-line query.

For each query (x, y) starting at the end of the s[y] node, the end node of the s[x] with the DFS sequence idea and the tree array, the node of the fail subtree contains several s[y] nodes, which is the answer.

At that time AC automata will not, failtree not, suffix array no, DFS sequence didn't think ... Qaq

1#include <algorithm>2#include <cstdio>3#include <iostream>4#include <cstdlib>5#include <cstring>6#include <vector>7#include <queue>8 9 #defineRep (I, L, R) for (int i = l; I <= R; i++)Ten #defineDown (i, L, R) for (int i = l; I >= R; i--) One #defineMS 123456 A #defineMAX 1037471823 -  - using namespacestd; the  - structnode - {  -     intx, y, N; +     BOOL operator< (ConstNode &k)Const{returnX <k.x;}; -} E[ms];intEC, Fir[ms]; + intC[MS], r[ms][ -], F[ms], H[ms], TC, SS[MS], Ds[ms], Dt[ms], k[ms]; Aqueue<int>p; at intN, m, x, Y, now, NQ; - CharS[ms]; -  - voidDFS (intx) - { -     into = fir[x]; DS[X] = + +Now ; in      while(o) {DFS (E[O].Y); o =E[O].N;} -DT[X] =Now ; to } +  -InlinevoidADD (intXintz) the{into = x; while(o<=tc+1) k[o]+=z, o = o+ (o& (-o)); } *  $InlineintQsum (intx)Panax Notoginseng { -     into = x, ans=0; while(o) ans+=k[o], o = O (o& (-o));  the     returnans; + } A  theInlinevoidQuery (intx) + { -      while(e[nq].x = =x) ${intY=E[NQ].Y; E[nq].y = Qsum (Dt[ss[y])-Qsum (ds[ss[y]]-1); nq++; } $ } -      - intMain () the { -scanf"%s", s);intL=strlen (s); f[0] = -1;WuyiRep (I,0, L-1)  the         if(s[i]!='P'&& s[i]!='B')  -{if(!r[now][s[i]-'a']) tc++, r[now][s[i]-'a']=TC, H[tc]=now, c[tc]=s[i]-'a'; now=r[now][s[i]-'a']; } Wu         Else if(s[i]=='P') Ss[++n] =Now ; -         Elsenow =H[now]; AboutRep (I,0, TC) F[i] =-1; P.push (0); $      while(!p.empty ()) -     { -x =P.front (); P.pop (); -Rep (I,0, -)if(R[x][i]) A         { +             intFO =F[x]; the              while(Fo >=0) -{if(R[fo][i]) Break;ElseFO =F[FO];} $             if(Fo <0) F[r[x][i]] =0;ElseF[r[x][i]] =R[fo][i]; the P.push (R[x][i]); the         } the     } theDown (i, TC,1) E[i].y = i, E[I].N = Fir[f[i]], fir[f[i]] = i; now =0; DFS (0); -Rep (I,1, TC) Fir[i] =0; inscanf"%d", &m); theRep (I,1, M) the{scanf ("%d%d", &x, &y); e[i].x = y, e[i].y = x, E[I].N =i;} AboutSort (e+1, e+1+m); thenow=n=0; nq=1; Rep (I,0, L-1)  the         if(s[i]!='P'&& s[i]!='B') {now=r[now][s[i]-'a']; ADD (Ds[now],1); } the         Else if(s[i]=='P') Query (++n);Else{ADD (Ds[now],-1); now =H[now];} +Rep (I,1, m) e[i].x = E[I].N; Sort (e+1, e+1+M); Rep (I,1, m) printf ("%d\n", e[i].y); -     return 0; the}
View Code

String Part 1

Related Article

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.