"BZOJ3238" difference (suffix array, monotonic stack)

Source: Internet
Author: User
Tags ord

Test instructions

Idea: Apparently Len (T[i]) +len (T[j]) This part of and is certain

So the question is how to quickly find the sum of 22 LCP

Considering that they are ranked after using SA can be very convenient to find the LCP, and the answer has no effect, because the form is a number of

So use SA to find the height

Height of each position as an interval of LCP expands to the left and right, until Height[x]

This legal left interval is [x+1,i] and the right interval is [i,y-1]

Consider how to maintain a position that supports finding a small number nearest to the current position than the current position

This can be maintained with a two-time monotonic stack.

But also consider the case of repeated calculations, such as having multiple minimum values covering all areas

We can take an equal sign at the time of comparison, one at a time

1 varX,y,sa,rank,height,wc,wd,a:Array[0..600000] ofLongint;2L,r:Array[1..600000] ofInt64;3Stk:Array[1..600000] ofLongint;4 N,i,m,top:longint;5 Ans,j:int64;6 ch:ansistring;7 8 functionmin (x,y:longint): Longint;9 beginTen  ifX<y Thenexit (x); One exit (y); A End; -  - functionCMP (A,b,l:longint): boolean; the begin -Exit ((Y[a]=y[b]) and(y[a+l]=y[b+l])); - End; -  + procedureSwapvarx,y:longint); - varT:longint; + begin AT:=x; X:=y; y:=T; at End; -  - procedureGetsa (n:longint); - varI,j,p:longint; - begin -   fori:=0  toN-1  Do in  begin -x[i]:=A[i]; to Inc (Wc[x[i]); +  End; -   fori:=1  toM-1  Dowc[i]:=wc[i-1]+Wc[i]; the   fori:=n-1 Downto 0  Do *  begin $ Dec (wc[x[i]]);Panax Notoginsengsa[wc[x[i]]]:=i; -  End; thej:=1; p:=1; +   whileP<n Do A  begin thep:=0; +    forI:=n-j toN-1  Do -   begin $y[p]:=I; Inc (P); $   End; -    fori:=0  toN-1  Do -    ifSa[i]>=j Then beginY[p]:=sa[i]-j; Inc (P);End; the    fori:=0  toN-1  Dowd[i]:=X[y[i]]; -    fori:=0  toM-1  Dowc[i]:=0;Wuyi    fori:=0  toN-1  DoInc (Wc[wd[i]); the    fori:=1  toM-1  Dowc[i]:=wc[i-1]+Wc[i]; -    fori:=n-1 Downto 0  Do Wu   begin - Dec (wc[wd[i]]); Aboutsa[wc[wd[i]]]:=Y[i]; $   End; -    fori:=0  toN Doswap (x[i],y[i]); -p:=1; x[sa[0]]:=0; -    fori:=1  toN-1  Do A    ifCMP (sa[i-1],SA[I],J) Thenx[sa[i]]:=p-1 +     Else beginX[sa[i]]:=p; Inc (P);End; thej:=j<<1; -m:=p; $  End; the End; the  the proceduregetheight (n:longint); the varI,j,k:longint; - begin ink:=0; the   fori:=1  toN Dorank[sa[i]]:=i; the   fori:=0  toN-1  Do About  begin the   ifK>0  ThenDec (k); thej:=sa[rank[i]-1]; the    whileA[I+K]=A[J+K] DoInc (k); +height[rank[i]]:=K; -  End; the End;Bayi  the begin theAssign (input,'bzoj3238.in'); Reset (input); -Assign (output,'Bzoj3238.out'); Rewrite (output); - READLN (CH); then:=Length (CH); the   fori:=0  toN-1  DoA[i]:=ord (ch[i+1])-ord ('a')+1; thea[n]:=0; m:= -; theGetsa (n+1); - getheight (n); thestk[1]:=1; height[1]:=-maxlongint; top:=1; the   fori:=2  toN Do the  begin94    while(top>0) and(Height[i] DoDec (top); the   ifstk[top]=1  Thenl[i]:=2 the    Elsel[i]:=stk[top]+1; theInc (top); stk[top]:=i;98  End; About  -stk[1]:=n+1; height[n+1]:=-maxlongint; top:=1;101   forI:=nDownto 2  Do102  begin103    while(top>0) and(Height[i]<=height[stk[top]]) DoDec (top);104   ifstk[top]=n+1  Thenr[i]:=N the    Elser[i]:=stk[top]-1;106Inc (top); stk[top]:=i;107  End;108 109  theAns:=int64 (n1) *int64 (N) * (n+1)Div 2;111   fori:=2  toN Doans:=ans-2* (-l[i]+i+1) * (r[i]-i+1)*Height[i]; the// fori:=2  ton+1  DoWriteln (L[i],' ', R[i]);113 writeln (ans); the close (input); the close (output); the End.

"BZOJ3238" difference (suffix array, monotonic stack)

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.