Description
The repetition number of a string is defined as the maximum number R such that 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 are 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 will not be greater than 100,000.
The last test case is 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 repetition number. If there are multiple substrings of maximum repetition number, print the lexicographically smallest one.
Sample Input
ccabababc
daabbccaa
Sample Output
Case 1: ababab
Case 2: aa
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
typedef long long ll;
const int maxn=128;
const int N=1e5+7;
int wa[N],wb[N],wv[N],c[N];
int c0(int *r,int a,int b){
return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];
}
int c12(int k,int *r,int a,int b){
if(k==2)return (r[a]<r[b])||(r[a]==r[b]&&c12(1,r,a+1,b+1));
return (r[a]<r[b])||(r[a]==r[b]&&wv[a+1]<wv[b+1]);
}
void sort(int *r,int *a,int *b,int n,int m){
int i;
for(i=0;i<n;++i) wv[i]=r[a[i]];
for(i=0;i<m;++i) c[i]=0;
for(i=0;i<n;++i) ++c[wv[i]];
for(i=1;i<m;++i) c[i]+=c[i-1];
for(i=n-1;i>=0;--i) b[--c[wv[i]]]=a[i];
}
void dc3(int *r,int *sa,int n,int m){
int i,j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;
r[n]=r[n+1]=0;
for(i=0;i<n;++i)if(i%3!=0)wa[tbc++]=i;
sort(r+2,wa,wb,tbc,m);
sort(r+1,wb,wa,tbc,m);
sort(r,wa,wb,tbc,m);
for(p=1,rn[F(wb[0])]=0,i=1;i<tbc;++i)rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++;
if(p<tbc) dc3(rn,san,tbc,p);
else for(i=0;i<tbc;++i) san[rn[i]]=i;
for(i=0;i<tbc;++i) if(san[i]<tb) wb[ta++]=san[i]*3;
if(n%3==1) wb[ta++]=n-1;
sort(r,wb,wa,ta,m);
for(i=0;i<tbc;i++) wv[wb[i]=G(san[i])]=i;
for(i=0,j=0,p=0;i<ta&&j<tbc;++p)sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++];
for(;i<ta;++p) sa[p]=wa[i++];
for(;j<tbc;++p) sa[p]=wb[j++];
}
int r[N*3],sa[N*3],Rank[N],height[N],n;
void calheight(int *r,int *sa,int n){
int i,j,k=0;
for(i=0;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);
}
int dp[N][20];
void Build_ST(){
for(int i=1;i<=n;++i)dp[i][0]=height[i];
for(int j=1;(1<<j)<=n;++j)
for(int i=1;i+(1<<j)-1<=n;++i)dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
inline int query(int l,int r){
int k=0;
while(1<<(k+1)<=r-l+1)++k;
return min(dp[l][k],dp[r-(1<<k)+1][k]);
}
inline int lcp(int l,int r){
l=Rank[l],r=Rank[r];
if(l>r)swap(l,r);
return query(l+1,r);
}
char s[N];
int cas;
vector<int>q;
int main(){
while(gets(s),s[0]!='#'){
n=strlen(s);
for(int i=0;i<n;++i)r[i]=s[i];
for(int i=n;i<=3*n;++i)r[i]=0;
dc3(r,sa,n+1,maxn);
calheight(r,sa,n);
Build_ST();
int ans=0,ok=1;
for(int l=1;l<=n;++l)
for(int i=0;i+l<n;i+=l){
int k=lcp(i,i+l);
int res=i-(l-k%l);
k=k/l+1;
if(res>=0&&lcp(res,res+l)>=l)++k;
if(k>ans)ans=k,q.clear(),q.push_back(l);
else if(k==ans&&q.back()!=i)q.push_back(l);
}
for(int i=1;i<=n&&ok;++i)
for(int j=q.size()-1;~j;--j)if(lcp(sa[i],sa[i]+q[j])>=(ans-1)*q[j]){
printf("Case %d: ",++cas);
s[ans*q[j]+sa[i]]=0;puts(s+sa[i]);
ok=0;break;
}
}
return 0;
}