POJ 3693 Maximum repetition substring (suffix array +rmq)

Source: Internet
Author: User
Tags repetition

                                                                                                                                              maximum Repetition substring

The repetition number of a string is defined as the maximum number R Such so the string can be partitioned into R same consecutive substrings. For example, the repetition number of ' Ababab ' is 3 and ' Ababa ' is 1.

Given a string containing lowercase letters, you is to find a substring of it with maximum repetition number.

Input

The input consists of multiple test cases. Each test case contains exactly one line, which
Gives a non-empty string consisting of lowercase letters. The length of the string is not being greater than 100,000.

The last test was followed by a line containing a ' # '.

Output

For each test case, print a line containing the "test Case number" (beginning with 1) followed by the substring of maximum R Epetition number. If There is multiple substrings of maximum repetition number, print the lexicographically smallest one.

Sample Input
ccabababcdaabbccaa#
Sample Output
Case 1:abababcase 2:aa

Analysis: Test instructions is the continuous substring with the most repetition times.
You can enumerate the length l, find the maximum number of consecutive occurrences of a string of length L
If a string of length L is repeated, then Str[0],str[l],str[2*l] ... This character, there will certainly be two contiguous, on repeated occurrences of the string.
Then find the maximum common prefix for str[i*l],str[(i+1) *l], and you can find the length h of the repeating consecutive string that they matched backwards.
This time you also need to find out if it is able to match the characters forward.
Set k=l-h%l; Then it needs to be matched forward to a round of required characters for K
This translates into the ability to determine whether the maximum common prefix of str[i*l-k] and str[(i+1) *l-k] can reach h
Number of characters matched/l+1 to the number of occurrences of the current length L
When looking for the maximum public prefix, it is necessary to use RMQ for the height preprocessing operation.
When the maximum number of occurrences is found, the length of the maximum number of occurrences can be reached with the array record L (a string that facilitates the output of the dictionary order precedence)
Finally, we need to output the string with the smallest dictionary order, because the suffix rank is higher and the dictionary order is smaller.
So we iterate by the suffix ranking and find the string with the smallest dictionary order.
The code is as follows:
#include <cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>typedefLong Longll;using namespacestd;Const intmaxn=100010;intWA[MAXN],WB[MAXN],WV[MAXN],WS[MAXN];CharSTR[MAXN];intST[MAXN];intminsum[maxn][ -];voidRmq_in (intNum//pretreatment->o (NLOGN){     for(intj =1; J < -; ++j) for(inti =1; I <= num; ++i)if(i + (1<< j)-1<=num) {Minsum[i][j]= Min (Minsum[i][j-1], Minsum[i + (1<< (J-1))][j-1]); }}intRM Q_Q Uery (intSrcintdes) {        intMinn; intK= (int) (Log (des-src+1.0)/log (2.0)); Minn=min (minsum[src][k],minsum[des-(1&LT;&LT;K) +1][k]); returnMinn;}intcmpint*r,intAintBintl) {returnr[a]==r[b]&&r[a+l]==r[b+l];}voidDaConst CharR[],intSa[],intNintM//N is len+1,m generally larger than the largest number in the array{      inti,j,p,*x=wa,*y=wb,*T;  for(i=0; i<m; i++) ws[i]=0;  for(i=0; i<n; i++) ws[x[i]=r[i]]++;  for(i=1; i<m; i++) ws[i]+=ws[i-1];  for(i=n-1; i>=0; i--) sa[--ws[x[i]]]=i;  for(j=1, p=1; p<n; j*=2, m=p) { for(p=0, I=n-j; i<n; i++) y[p++]=i;  for(i=0; i<n; i++)if(SA[I]&GT;=J) y[p++]=sa[i]-K;  for(i=0; i<n; i++) wv[i]=X[y[i]];  for(i=0; i<m; i++) ws[i]=0;  for(i=0; i<n; i++) ws[wv[i]]++;  for(i=1; i<m; i++) ws[i]+=ws[i-1];  for(i=n-1; i>=0; i--) sa[--ws[wv[i]]]=Y[i];  for(t=x,x=y,y=t,p=1, x[sa[0]]=0, i=1; i<n; i++) X[sa[i]]=CMP (y,sa[i-1],sa[i],j)? p1:p + +; }      return;}intSA[MAXN],RANK[MAXN],HEIGHT[MAXN];//The SA is found in the position of the string by the suffix rank, where rank is the suffix rank based on the position, the inverse of which is the minimum value of the SA array in the template is 1. voidCalheight (Const Char*r,int*sa,intN) {      inti,j,k=0;  for(i=1; i<=n; i++) rank[sa[i]]=i;  for(i=0; i<n; height[rank[i++]]=k) for(k?k--:0, j=sa[rank[i]-1]; R[I+K]==R[J+K]; k++);}intCT[MAXN];intMain () {intT,len,maxx,r,a,b,c,h,q1,q2,tem,h2,ans,y,times,cnt=0, anslen,s,start,k2,case=0;  while(SCANF ("%s", str)! =EOF) { case++; Maxx=0; if(str[0]=='#') Break; Len=strlen (str); Da (Str,sa,len+1, the);      Calheight (Str,sa,len);  for(intI=2; i<len;i++) minsum[i][0]=Height[i];      Rmq_in (len);  for(intk=1; k<len;k++)      {           for(intj=0; j<len;j+=k) {if(j+k>=len) Break; A=min (rank[j],rank[j+K]); b=max (rank[j],rank[j+K]); H=rm Q_Q Uery (A +1, B); Times=h/k+1; Y=h%K; if(J-(K-h%k) >=0) {Q1=j-(k-h%k); Q2=j+k-(k-h%k); intA=min (rank[q1],rank[q2]); intb=Max (RANK[Q1],RANK[Q2]); H2=rm Q_Q Uery (A +1, B); if(h2>=h) times++; }             if(times>Maxx) {Maxx=Times ; CNT=0; }             if(times==Maxx) st[cnt++]=K; }           //Maxx=max (Ans,maxx);} Anslen=-1;  for(intI=1; i<=len&&anslen==-1; i++)             for(intj=0; j<cnt;j++)            {                intA=min (i,rank[sa[i]+St[j]]); intB=max (i,rank[sa[i]+St[j]]); S=rm Q_Q Uery (A +1, B); if(S>= (maxx-1)*St[j]) {Start=Sa[i]; //cout<< "start=" <<start<<endl;anslen=maxx*St[j];  Break; }} printf ("Case %d:", case);  for(inti=start;i<start+anslen;i++) printf ("%c", Str[i]); printf ("\ n"); }return 0;}



POJ 3693 Maximum repetition substring (suffix array +rmq)

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.