Portal: http://poj.org/problem?id=3693
Title: Give a string, the repetition of the most repeated number of consecutive substrings;
Analysis:
Enumerates the length of a repeating cell, and then naturally enumerates the starting point. With RMQ processing, the longest prefix of the suffix i,i+l is used.
Lcp/l+1, the number of repetitions of the string for the current adjacent L-length unit, but because of the i+=l, improves the efficiency, but I do not necessarily happen to be the starting point of the repeating string, so if r%l! =0, move the string forward l-r%l units. The maximum number of repetitions is found, and then the SA array is used to rank (1-n), which can be output in dictionary order.
Detailed Link: http://blog.csdn.net/acm_cxlove/article/details/7941205
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include < algorithm> #include <vector> #include <string> #include <queue> #include <deque> #include <stack> #include <map> #include <set> #define INF 0x7fffffff#define SUP 0x80000000#define Mem (A, B) memset (A,b,sizeof (a)) using namespace Std;typedef long long ll;const int n=100007;/******************auther:acsorry*** The following is a multiplication algorithm for the suffix array int wa[n],wb[n],wv[n],ws[n];int cmp (int *r,int a,int b,int l) {return r[a]==r[b]&&am P;R[A+L]==R[B+L];} void da (const char *r,int *sa,int n,int m) {int i,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]>=j) y[p++]=sa[i]-j;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)? p-1:p++;} return;} int sa[n],rank[n],height[n];//height array void calheight (const char *r,int *sa,int N) {int i,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++); return;} int dp[n][20];void rmq_init (int N) {int M=floor (log (n+0.0)/log (2.0)); for (int i=1;i<=n;i++) dp[i][0]=height[i]; for (int j=1;j<=m;j++) {for (int i=1;i+ (1<<J) -1<=n;i++) {dp[i][j]=min (dp[i][j-1],dp[i+ (1<&L t; (j-1))][j-1]); }}}int rmq_query (int l,int R) {int a=rank[l],b=rank[r]; if (a>b) swap (A, b); a++;//this place + +, should be for height[a] processing is a,a-1 common string int m=floor ((log (b-a+1.0)/log (2.0)); return min (dp[a][m],dp[b-(1<<m) +1][m]);} Char Str[n];int main () {int cas=0; while (scanf ("%s", str) ==1&&str[0]!= ' # ') {int n=strlen (str); Da (str,sa,n+1,130); Calheight (Str,sa,n); Rmq_init (n); int cnt=0,maxx=0,a[n]; for (int l=1;l<n;l++) {for (int i=0;i+l<n;i+=l) {//j+=l. The complexity needs int r=rmq_query (I,I+L); int step=r/l+1; int k=i-(L-R%L); if (k>=0&&r%l) {if (Rmq_query (k,k+l) >=r) step++; } if (Step>maxx) {maxx=step; cnt=0; A[cnt++]=l; } else if (Step==maxx) a[cnt++]=l; }} int len=-1,st; for (int i=1;i<=n&&len==-1;i++) {for (int j=0;j<cnt;j++) {if (Rmq_query (sa[i],sa[i]+a [j]) /a[j]+1==maxx) {len=a[j]; St=sa[i]; Break }}} Str[st+maxx*len]=0; printf ("Case%d:%s\n", ++cas,str+st); } return 0;}
I would rather have a wonderful life than a mediocre life.
POJ 3693 Maximum Repetition substring (08 Hefei rmq+ suffix Array)