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
思路:枚举重复长度,求出整个串的最大重复次数。重复次数=LCP(i,i+重复长度)/重复长度。当不能整除时需要移位再算一下。
#include<iostream> #include<cstring> #include<cstdio> #define ll(x) (1<<x) #define FOR(i,a,b) for(int i=a;i<=b;++i) #define clr(f,z) memset(f,z,sizeof(f)) using namespace std; const int msize=1e5+9; class SUFFIX_ARRAY { public: int rank[msize],h[msize],c[msize],sa[msize]; bool cmp(int*r,int i,int k) { return r[ sa[i] ]==r[ sa[i-1] ]&&r[ sa[i]+k ]==r[ sa[i-1]+k ]; } void build_SA(char*s,int n,int m) { int*wx=h,*wy=rank; FOR(i,0,m-1)c[i]=0; FOR(i,0,n-1)++c[ wx[i]=s[i] ]; FOR(i,1,m-1)c[i]+=c[i-1]; for(int i=n-1;i>=0;--i)sa[ --c[ wx[i] ] ]=i; for(int k=1;k<=n;k<<=1) { int p=0; FOR(i,n-k,n-1)wy[p++]=i; FOR(i,0,n-1)if(sa[i]>=k)wy[p++]=sa[i]-k; FOR(i,0,m-1)c[i]=0; FOR(i,0,n-1)++c[ wx[ wy[i] ] ]; FOR(i,1,m-1)c[i]+=c[i-1]; for(int i=n-1;i>=0;--i)sa[ --c[ wx[ wy[i] ] ] ]=wy[i]; swap(wx,wy); p=1;wx[ sa[0] ]=0; FOR(i,1,n-1)wx[ sa[i] ]=cmp(wy,i,k)?p-1:p++; if(p>=n)break; m=p; } } void get_H(char*s,int n) { int k=0; FOR(i,0,n)rank[ sa[i] ]=i; FOR(i,0,n-1) { if(k)--k; int j=sa[ rank[i]-1 ]; while(s[i+k]==s[j+k])++k; h[ rank[i] ]=k; } } void debug(int n) { printf("sa="); FOR(i,0,n)printf("%d ",sa[i]);puts(""); printf("rank="); FOR(i,0,n)printf("%d ",rank[i]);puts(""); printf("h="); FOR(i,0,n)printf("%d ",h[i]);puts(""); } int rmq[msize][33],bit[msize]; void initRMQ(int n) { bit[0]=-1; FOR(i,1,n)bit[i]=(i&(i-1))==0?bit[i-1]+1:bit[i-1]; FOR(i,1,n)rmq[i][0]=h[i]; FOR(i,1,bit[n]) for(int j=1;j+ll(i)-1<=n;++j) rmq[j][i]=min(rmq[j][i-1],rmq[j+ll(i-1)][i-1]); } int LCP(int l,int r) { l=rank[l],r=rank[r]; if(l>r)swap(l,r); ++l; int t=bit[r-l+1]; r-=ll(t)-1;///这个居然忘了 return min(rmq[l][t],rmq[r][t]); } int has[msize]; void getstring(char*s,int n) { int Max=0,pos=0; FOR(l,1,n-1)///重复的串长 for(int i=0;i+l<n;i+=l) { int t=LCP(i,i+l); int num=t/l+1; int k=i-(l-t%l);///需要右移匹配的串长 if(k>=0&&t%l!=0) { if(LCP(k,k+l)>=t)++num;//还有一个 } if(num>Max)Max=num,pos=0,has[pos++]=l; else if(num==Max)has[pos++]=l; } // cout<<Max<<" "<<has[pos-1]<<endl; int len=-1,st; for(int i=1;i<=n&&len==-1;++i) FOR(j,0,pos-1) { int l=has[j]; if(LCP(sa[i],sa[i]+l)>=(Max-1)*l) {// printf("%s\n",s+sa[i]); // printf("i-%d %d %d %d\n",i,LCP(sa[i],sa[i]+l),sa[i],l); // cout<<LCP(13,14)<<endl; len=l;st=sa[i]; break; } } s[st+len*Max]=0; printf("%s\n",s+st); } }; SUFFIX_ARRAY ty; char s[msize]; int r[msize]; int main() { int cas=0; while(~scanf("%s",s)) { if(s[0]=='#')break; printf("Case %d: ",++cas); int len=strlen(s); ty.build_SA(s,len+1,128); ty.get_H(s,len); ty.initRMQ(len); //ty.debug(len); int a,b; //while(scanf("%d%d",&a,&b))printf("lcp=%d\n",ty.LCP(a,b)); ty.getstring(s,len); } }