"POJ3693" Maximum repetition substring (SA)

Source: Internet
Author: User
Tags repetition

This is a magical topic. The paper is not clear, in fact, so ... If a string of length l is repeated multiple times, then at least s[1],s[l+1],s[2*l+1],.. There are 2 adjacent equal ...  Set at this time for j=i*l+1,k=j+l, we use SA and RMQ O (1) to find out: M=LCP (j,k), at this time, the number of repetitions ans=m div l+1. Of course, we enumerate to not necessarily be optimal, because you enumerate not necessarily the end of the string. What happens then? That's what the paper says, forward and backward. We set T=L-M mod L. It can be understood that, at this time, M mod L is more than the characters, T is considered to be the number of fewer characters in front. Of course, if M mod l=0 does not have to control this ... That is to say, we again judge whether LCP (J-T,K-T) >=l if set up, then ans++ ... Because there can be more than a paragraph.

This solves the longest problem, the following will be about the dictionary order of this ... In fact, SA is the dictionary order, as long as the enumeration is LCP (SA[I],SA[I]+L) >= (ans-1) *l, if established, obviously the current sa[i] starting length of the string is the answer ...

Well.. After writing, suddenly feel the rise posture.

Constmaxn=100419;varRec,c,h,rank,sa,x,y:Array[0.. MAXN] oflongint; F:Array[0.. MAXN,0.. -] oflongint; n,cas:longint; s:ansistring ;functionMax (x,y:longint): Longint;begin ifX>y ThenExit (x)ElseExit (y);End;functionMin (x,y:longint): Longint;begin ifX<y ThenExit (x)ElseExit (y);End;procedureSwapvarX,y:longint);varTmp:longint;begintmp:=x;x:=y;y:=tmp;End;procedureMake ;varP,i,tot:longint;beginP:=1;  whileP<n Do  beginFillchar (C,sizeof (c),0);  fori:=1  toN-p Doy[i]:=rank[i+p];  fori:= n-p+1  toN Doy[i]:=0;  fori:=1  toN DoInc (C[y[i]);  fori:=1  toN DoInc (c[i],c[i-1]);  fori:=1  toN Do    beginSa[c[y[i]]:=i;    Dec (C[y[i]]); End; Fillchar (C,sizeof (c),0);  fori:=1  toN Dox[i]:=Rank[i];  fori:=1  toN DoInc (C[x[i]);  fori:=1  toN DoInc (c[i],c[i-1]);  fori:= NDownto 1  Do    beginY[sa[i]]:=C[x[sa[i]];    Dec (C[x[sa[i]]); End;  fori:=1  toN Dosa[y[i]]:=i; Tot:=1; rank[sa[1]]:=1;  fori:=2  toN Do    begin     if(x[sa[i]]<>x[sa[i-1]])or(x[sa[i]+p]<>x[sa[i-1]+P]) ThenInc (TOT); Rank[sa[i]]:=tot; End; P:=p<<1; End;  fori:=1  toN Dosa[rank[i]]:=i;End;procedureMakeh;varI,j,p:longint;beginh[1]:=0; p:=0;  fori:=1  toN Do  beginP:=max (P-1,0); ifrank[i]=1  Thencontinue; J:=sa[rank[i]-1];  while(i+p<=n) and(j+p<=n) and(S[i+p]=s[j+p]) DoInc (P); H[rank[i]]:=p; End;End;procedureRMQ;varI,j:longint;begin  fori:=1  toN DoF[i,0]:=H[i]; fori:=1  toTrunc (ln (n)/ln (2)) Do   forj:=1  toN-1<<i+1  DoF[j,i]:=min (f[j,i-1],f[j+1<< (I-1), I-1]);End;procedureInit;varI,tot:longint;beginN:=length (s); fori:=1  toN Dox[i]:=Ord (s[i]); Fillchar (C,sizeof (c),0);  fori:=1  toN DoInc (C[x[i]); fori:=1  to  -  DoInc (c[i],c[i-1]);  fori:=1  toN Do  beginSa[c[x[i]]:=i;  Dec (C[x[i]]); End; rank[sa[1]]:=1; tot:=1;  fori:=2  toN Do  begin   ifx[sa[i]]<>x[sa[i-1]] ThenInc (TOT); Rank[sa[i]]:=tot; End; make ; makeh; rmq ;End;functionLCP (X,y:longint): Longint;varT:longint;beginx:=RANK[X]; y:=Rank[y];ifX>y Thenswap (x, y);ifX<y ThenInc (X); t:=trunc (LN (y-x+1)/LN (2)); Exit (min (f[x,t],f[y-1<<t+1, T]));End;proceduresolve;varm,l,i,j,tmp,t,ans,cnt:longint; pd:boolean;begininit; Fillchar (rec,sizeof (rec),0); Ans:=0;  forl:=1  toN-1  Do  beginI:=1;  whileI+l<=n Do    beginm:=LCP (i,i+l); TMP:=mDivL +1; T:=l-mMoDl; T:=i-T; if(t>0) and(MMoDL<>0) and(LCP (T,T+L) >=m) ThenInc (TMP); ifTmp>ans Then      beginCNT:=1; rec[1]:=M; Ans:=tmp; End; ifCnt=ans Then      beginInc (CNT); REC[CNT]:=l; End; I:=i+l; End; End; PD:=false; fori:=1  toN Do  if  notPd Then    forj:=1  toCnt Do    beginL:=Rec[j]; ifLCP (Sa[i],sa[i]+l) >= (ans-1) *l Then      beginT:=Sa[i]; L:=l*ans; PD:=true;      Break End; End  ElseBreak ; Inc (CAs); write (' Case'Cas': ');  fori:= T tot+l-1  Dowrite (s[i]); writeln;End; Begin CAs:=0; Readln (s); whiles[1]<>'#'  Do  beginsolve;  READLN (s); End; End.

"POJ3693" Maximum repetition substring (SA)

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.