POJ3693 Maximum repetition substring 后缀数组

来源:互联网 发布:软件开发环境 编辑:程序博客网 时间:2024/04/24 10:15

题目链接:POJ3693

Maximum repetition substring
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 9292 Accepted: 2855

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

ccabababcdaabbccaa#

Sample Output

Case 1: abababCase 2: aa
题意:求重复次数最多的连续重复子串(雾)。换句话说就是找这个子串是循环子串且循环次数最多,如果有多个输出字典序最小的。
题目分析:首先枚举长度l作为循环节长度,找到循环次数最多的一项,由于循环节是l,则在s[0],s[l],s[2*l].....中必然有连续的2个相同,匹配从kl开始和l(k+1)开始的lcp,那么循环次数就是lcp/l;但这kl不一定是循环节的起始点,通过lcp%l可以找到起始点,再匹配一遍来确定最大的循环长度。
之后遍历sa数组来找最大的循环子串,由于sa数组已经排好序,找到的第一个即可。
////  main.cpp//  POJ3693////  Created by teddywang on 2016/10/1.//  Copyright © 2016年 teddywang. All rights reserved.//#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;const int maxn=100010;int t1[maxn],t2[maxn],c[maxn];bool cmp(int *r,int a,int b,int l){    return r[a]==r[b]&&r[a+l]==r[b+l];}void da(int str[],int sa[],int ranks[],int height[],int n,int m){    n++;    int i,j,p,*x=t1,*y=t2;    for(i=0;i<m;i++) c[i]=0;    for(i=0;i<n;i++) c[x[i]=str[i]]++;    for(i=1;i<m;i++) c[i]+=c[i-1];    for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;    for(j=1;j<=n;j<<=1)    {        p=0;        for(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<m;i++) c[i]=0;        for(i=0;i<n;i++) c[x[y[i]]]++;        for(i=1;i<m;i++) c[i]+=c[i-1];        for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];        swap(x,y);        p=1;x[sa[0]]=0;        for(i=1;i<n;i++)            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;        if(p>=n) break;        m=p;    }    int k=0;    n--;    for(i=0;i<=n;i++)ranks[sa[i]]=i;    for(i=0;i<n;i++)    {        if(k)k--;        j=sa[ranks[i]-1];        while(str[i+k]==str[j+k])k++;        height[ranks[i]]=k;    }}int ranks[maxn],height[maxn];char str[maxn],s1[maxn];int r[maxn],sa[maxn];int pre[maxn],vis[maxn];int RMQ[maxn],mm[maxn];int dp[maxn][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 i=1;i<=m;i++)    {        for(int j=n;j;j--){            dp[j][i]=dp[j][i-1];            if(j+(1<<(i-1))<=n)                dp[j][i]=min(dp[j][i],dp[j+(1<<(i-1))][i-1]);        }    }}int Rmq_Query(int l,int r){    int a=ranks[l],b=ranks[r];    if(a>b) swap(a,b);    a++;    int m=floor(log(b-a+1.0)/log(2.0));    return min(dp[a][m],dp[b-(1<<m)+1][m]);}int main(){    int kase=1;    while(scanf("%s",str))    {        if(str[0]=='#') break;        int len=strlen(str);        for(int i=0;i<len;i++) r[i]=str[i];        r[len]=0;        da(r,sa,ranks,height,len,128);        Rmq_Init(len);        int cnt=1,mmax=1,a[maxn];        a[0]=1;        for(int i=1;i<len;i++)        {            for(int j=0;i+j<len;j+=i)            {                int r=Rmq_Query(j, j+i);                int steps=r/i+1;                int k=j-(i-r%i);                if(k>=0&&r%i)                    if(Rmq_Query(k, k+i)>=r) steps++;                if(steps>mmax)                {                    mmax=steps;                    cnt=0;                    a[cnt++]=i;                }                else if(steps==mmax)                    a[cnt++]=i;            }        }        int n=-1,st=0;        for(int i=1;i<=len&&n==-1;i++)        {a\            for(int j=0;j<cnt;j++)            {                int l=a[j];                if(Rmq_Query(sa[i], sa[i]+l)>=(mmax-1)*l)                {                    n=l;                    st=sa[i];                    break;                }            }        }        printf("Case %d: ",kase++);        for(int i=st,j=0;j<n*mmax;j++,i++) printf("%c",str[i]);        printf("\n");    }}



0 0