poj3693

http://www.elijahqi.win/2017/07/23/poj3293/
Maximum repetition substring
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10165 Accepted: 3125
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
Source

2008 Asia Hefei Regional Contest Online by USTC
先穷举长度L,然后求长度为L 的子串最多能连续出现几次。首先连续出现1 次是肯定可以的,所以这里只考虑至少2 次的情况。假设在原字符串中连续出现2 次,记这个子字符串为S,那么S 肯定包括了字符r[0], r[L], r[L*2],r[L*3], ……中的某相邻的两个。所以只须看字符r[L*i]和r[L*(i+1)]往前和往后各能匹配到多远,记这个总长度为K,那么这里连续出现了K/L+1 次。
以上来自罗穗骞论文

论文里的原图

可以画一下图(借用了其他blog 的图)

我们枚举len,然后把原始串划分成Len长度的若干个串,然后枚举块头第一个元素,然后每次都和后一个块比较,为什么只和i+1比较,因为 枚举的是长度,总会枚举到不同的

对于当前的LEN,i,i+1,x=s[i*L],y=s[(i+1)*L],找前找后,知道了最早能匹配到op,最晚能匹配到cl,因为不知道当前的起始点是真正循环节的第几个点,所以我们要往前找L个点看看它们是不是真正的起始点。

随着循环节前端的调整,后端也有可能发生变化ar=op+sub*p-1 所以我们记录后端点的时候应该从当前前端开始,枚举sub个块截止到op+块数*LEN-1;

这就是枚举块加入发生偏移的解决方法

用rmq求一个lcp 这题求重复次数最多 的连续重复子串 非常绕

求lcp的过程可以发现,这其实和kmp求循环节非常相似

#include<cstdio>
#include<cstring>
#define N 110000
int T,n,m,k;
int rank[N<<1],rank1[N],sa[N],tmp[N],height[N],count[N],f[N][30];
char a[N];
inline int min(int x,int y){
    return x<y?x:y;
}
inline void swap(int &x,int &y){
    int t=x;x=y;y=t;
}
inline int qrmq(int x,int y){
    if (x>y) swap(x,y);
    x++;int k=0,tmp=y-x+1;
    while ((1<<k+1)<=tmp) ++k;
    return min(f[x][k],f[y-(1<<k)+1][k]);
}
int main(){
    freopen("poj3693.in","r",stdin);
//  freopen("poj3693.out","w",stdout);
    while (1){
        scanf("%s",a+1);
        n=strlen(a+1);
        if (a[1]=='#') break;
        printf("Case %d: ",++T);
        memset(count,0,sizeof(count));
        memset(rank,0,sizeof(rank));
        for (int i=1;i<=n;++i) count[a[i]]=1;
        for (int i=1;i<=255;++i) count[i]+=count[i-1];
        for (int i=1;i<=n;++i) rank[i]=count[a[i]];
        m=30;k=0;
        for (int p=1;k!=n;p<<=1,m=k){
            for (int i=1;i<=m;++i) count[i]=0;
            for (int i=1;i<=n;++i) count[rank[i+p]]++;
            for (int i=1;i<=m;++i) count[i]+=count[i-1];
            for (int i=n;i>=1;--i) tmp[count[rank[i+p]]--]=i;
            for (int i=1;i<=m;++i) count[i]=0;
            for (int i=1;i<=n;++i) count[rank[i]]++;
            for (int i=1;i<=m;++i) count[i]+=count[i-1];
            for (int i=n;i>=1;--i) sa[count[rank[tmp[i]]]--]=tmp[i];
            memcpy(rank1,rank,sizeof(rank)>>1);
            rank[sa[1]]=k=1;
            for (int i=2;i<=n;++i){
                if (rank1[sa[i]]!=rank1[sa[i-1]]||rank1[sa[i-1]+p]!=rank1[sa[i]+p]) ++k;
                rank[sa[i]]=k;
            }
        }
        k=0;
        for (int i=1;i<=n;++i){
            if (rank[i]==1) continue;
            k=k==0?0:k-1;
            while (a[i+k]==a[sa[rank[i]-1]+k])++k;
            height[rank[i]]=k;
        }
        //求RMQ
        for (int i=1;i<=n;++i) f[i][0]=height[i];
        for (int i=1;1<<i<=n;++i){
            for (int j=1;j+(1<<i)<=n+1;++j){
                f[j][i]=min(f[j][i-1],f[j+(1<<i-1)][i-1]);
            }
        }
    /*  for (int i=1;1<<i<=n;++i){
            for (int j=1;j+(1<<i)<=n+1;++j){
                printf("%d ",f[j][i]);
            }
            printf("\n");
        }  */
        int ans=0,op,cl,al,ar;
        for (int p=1;p<=n>>1;++p){
            for (int i=0;p*(i+1)+1<=n;++i){
                int p1=p*i+1,p2=p*(i+1)+1;
                if (a[p1]!=a[p2]) continue;
                int minn=qrmq(rank[p1],rank[p2]);
                op=0;cl=minn+p2-1;
                for (int j=0;j<p;++j){
                    if (p1-j<1||a[p1-j]!=a[p2-j]) break;
                    op=p1-j;
                    int sub=(cl-op+1)/p;
                    if (sub>ans||sub==ans&&(rank[op]<rank[al])) ans=sub,al=op,ar=op+sub*p-1;
                }
            }
        }
        if (ans==0) printf("%c\n",a[sa[1]]);else{
            for (int i=al;i<=ar;++i) printf("%c",a[i]);printf("\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值