COJ975 WZJ data structure (minus 25)

Source: Internet
Author: User

Question Description

Enter a string s, answer q questions, give you l,r, output subsequence [l,r] the longest continuous palindrome string length.

Input

The first behavior is a string s.

The second behavior is a positive integer q.

Next the Q line is l,r per behavior.

Output

For each query, output the answer.

Input example

Aababababaabababaaa

4

1 3

2 6

1 10

2 7

Output example

2

5

9

5

Other Notes

1<=| s|,q<=100000

1<=l<=r<=| s|

Since it is obviously not easy to find the longest palindrome string length of the interval, we consider using chunking to maintain the answer.

We divide each size element into pieces, asking for answers between chunks and small contributions.

The former can be preprocessed (horse-drawn or Pam), time-complexity O (n^2/size).

The latter we can use Pam, when the palindrome string of a paragraph in a small section, it must be left in small segments, right side in the right small segment. The two are symmetrical, and we begin to consider how to find the answer to the right side of the palindrome string.

Preprocessing the nodes on the PAM at each location, but this may exceed the ask boundary, so we have to keep going up and down the mismatch edge when the length of the node <= the length of the limit.

If violence is good for random data, it will be a special data card. So we can do it with multiplication, so the time complexity of this part is O (q*size*logsize).

The total time complexity is O (n^2/size+q*size*logsize), the measured SIZE takes [150,200].

#include <cstdio>#include<cctype>#include<queue>#include<cmath>#include<stack>#include<cstring>#include<algorithm>#defineRep (i,s,t) for (int i=s;i<=t;i++)#defineren for (int i=first[x];i;i=next[i])using namespaceStd;inlineintRead () {intx=0, f=1;CharC=GetChar ();  for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1;  for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; returnx*F;}Const intmaxn=100010;CharCH[MAXN];intLpos[maxn],rpos[maxn],nowret;structPAM {intlast,cnt,to[maxn][ -],L[MAXN],FA[MAXN]; intfirst[maxn],next[maxn],to[maxn],anc[maxn][ -],e; voidinit () {memset (To,0,sizeof(to)); memset (L,0,sizeof(l)); memset (FA,0,sizeof(FA)); memset (First,0,sizeof(first)); CNT=fa[0]=1; l[1]=-1; last=e=0; }    voidExtendintCintNintID) {intp=Last ;  while(ch[n]!=ch[n-l[p]-1]) p=Fa[p]; if(!To[p][c]) {            intnp=++cnt,k=fa[p];l[np]=l[p]+2;  while(ch[n]!=ch[n-l[k]-1]) k=Fa[k]; FA[NP]=to[k][c];to[p][c]=NP; } Nowret=max (nowret,l[last=To[p][c]]); if(id>0) lpos[id]=Last ; Elserpos[-id]=Last ; } Stack<int>S; voidDfsintx) {s.push (x);  while(!S.empty ()) {x=S.top ();            S.pop (); anc[x][0]=Fa[x]; Rep (I,1, +) anc[x][i]=anc[anc[x][i-1]][i-1];        ren S.push (to[i]); }    }    voidAddedge (intUintv) {to[++e]=v;next[e]=first[u];first[u]=e; }    voidbuild () {fa[1]=1; Rep (I,0, CNT)if(i!=1) Addedge (fa[i],i); DFS (1); }    intSolveintXintlimit) {        if(L[x]<=limit)returnL[x];  for(intI= +; i>=0; i--)if(L[anc[x][i]]>limit) x=Anc[x][i]; returnL[fa[x]]; }}sol1,sol2;intn,size;intans[ -][ -],BL[MAXN],ST[MAXN],EN[MAXN];intMain () {scanf ("%s", ch+1); N=strlen (ch+1); Size= Max; Rep (I,1, N) {Bl[i]= (I-1)/size+1; En[bl[i]]=i; if(!st[bl[i]]) st[bl[i]]=i; } Rep (I,1, Bl[n]) {sol1.init (); Nowret=0; Charc=ch[st[i]-1];ch[st[i]-1]='~'; Rep (j,st[i],n) {sol1.extend (Ch[j]-'a'J1); ANS[I][BL[J]]=Max (Ans[i][bl[j]],nowret); } Ch[st[i]-1]=C;    } sol1.init (); Sol2.init (); Rep (I,1, N) sol1.extend (ch[i]-'a', I,i); Reverse (Ch+1, ch+n+1); Rep (I,1, N) sol2.extend (ch[i]-'a', i,-(n-i+1));    Sol1.build (); Sol2.build (); intm=read ();  while(m--) {        intL=read (), R=read (), ret=0; if(bl[l]+1&GT;=BL[R]) Rep (i,l,r) Ret=max (Ret,sol1.solve (lpos[i],i-l+1)); Else{ret=ans[bl[l]+1][bl[r]-1]; Rep (I,l,en[bl[l]]) ret=max (Ret,sol2.solve (rpos[i],r-i+1)); Rep (i,st[bl[r]],r) ret=max (Ret,sol1.solve (lpos[i],i-l+1)); } printf ("%d\n", ret); }    return 0;}
View Code

COJ975 WZJ data structure (minus 25)

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.