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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值