poj-3693(字尾陣列+RMQ)
poj-3693 Maximum repetitiion substring
題目:
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
和上一題一樣板子題,,不過這題比上題多了個輸出字典序最小,這就用到rank陣列進行比較,然後在尋找字串的時候需要往前遍歷到k-1位找到字典序最少的字串,對只能重複一次的串特殊處理一下找到最小的字母把它輸出。
AC程式碼:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<complex>
#include<queue>
#define T 111111
using namespace std;
char s[T];
int t1[T],t2[T],cc[T],x[T],sa[T],rnk[T],height[T];
int f[T][20];
int len;
int ans;
bool cmp(int *y,int a,int b,int k)
{
int a1=y[a];
int b1=y[b];
int a2=a+k>=len ? -1:y[a+k];
int b2=b+k>=len ? -1:y[b+k];
return a1==b1 && a2==b2;
}
int make_sa()
{
int *x=t1,*y=t2;
int m=255;
for(int i=0; i<m; i++) cc[i]=0;
for(int i=0; i<len; i++) ++cc[x[i]=s[i]];
for(int i=1; i<m; i++) cc[i]+=cc[i-1];
for(int i=len-1; i>=0; i--) sa[--cc[x[i]]]=i;
for(int k=1; k<=len; k<<=1)
{
int p=0;
for(int i=len-k; i<len; i++) y[p++]=i;
for(int i=0; i<len; i++)
if( sa[i]>=k ) y[p++]=sa[i]-k;
for(int i=0; i<m; i++) cc[i]=0;
for(int i=0; i<len; i++) ++cc[x[y[i]]];
for(int i=1; i<m; i++) cc[i]+=cc[i-1];
for(int i=len-1; i>=0; i--) sa[--cc[x[y[i]]]]=y[i];
swap(x,y);
m=1; x[sa[0]]=0;
for(int i=1; i<len; i++)
x[sa[i]]=cmp(y,sa[i],sa[i-1],k) ? m-1:m++;
if( m>=len ) break;
}
}
void make_height()
{
for(int i=0; i<len; i++) rnk[sa[i]]=i;
height[0]=0;
int k=0;
for(int i=0; i<len; i++)
{
if(!rnk[i]) continue;
int j=sa[rnk[i]-1];
if(k) k--;
while(s[i+k]==s[j+k]) k++;
height[rnk[i]]=k;
}
}
void rmq()
{
for(int i=0;i<len;i++){
f[i][0]=height[i];
}
int nlog=int(log(double(len))/log(2.0));
for(int j=1;j<=nlog;j++){
for(int i=0;i<len;i++){
if(i+(1<<j)<=len){
f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
}
}
}
}
int qrmq(int l,int r)
{
l=rnk[l];
r=rnk[r];
if(l>r){
int h;
h=l;l=r;r=h;
}
l++;
int nlog=int (log(double(r-l+1))/log(2.0));
return min(f[l][nlog],f[r-(1<<nlog)+1][nlog]);
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t=1;
while(scanf("%s",s)!=EOF)
{
if(s[0]=='#') break;
len=strlen(s);
make_sa();
make_height();
rmq();
ans=0;
int ansk=0,anspos=0;
for(int k=1;k<len;k++){
for(int i=0;i+k<len;i+=k){
int n=qrmq(i,i+k);
n--;
for(int j=0;j<=k-1;j++){
int now=i-j;
if((now<0||s[now]!=s[now+k])&&j) break;
n++;
int sum=n/k+1;
if(sum>ans||(sum==ans&&rnk[now]<rnk[anspos])){
ans=sum;
anspos=now;
ansk=k;
}
}
}
}
printf("Case %d: ",t++);
if(ans<=1) {
int minn=999;
for(int i=0;i<len;i++){
minn=min(minn,int(s[i]));
}
printf("%c\n",minn);
continue;}
for(int i=0;i<ansk*ans;i++){
printf("%c",s[i+anspos]);
}
printf("\n");
}
return 0;
}