zoukankan      html  css  js  c++  java
  • poj3693 Maximum repetition substring (后缀数组+rmq)

    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

    题意:和spoj687题意相同,只是最后要输出满足重复次数最多的字典序最小的字符串。我们可以把重复次数最多的循环长度保存下来,然后根据sa数组依次找开头的字母,判断下以这个位置字母为首字母,循环长度为保存下的数组中的其中一个值是否符合要求,如果符合就break。

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<string>
    #include<bitset>
    #include<algorithm>
    using namespace std;
    #define lson th<<1
    #define rson th<<1|1
    typedef long long ll;
    typedef long double ldb;
    #define inf 99999999
    #define pi acos(-1.0)
    #define maxn 100010
    int sa[maxn],a[maxn];
    int wa[maxn],wb[maxn],wv[maxn],we[maxn];
    int rk[maxn],height[maxn];
    int cmp(int *r,int a,int b,int l){
        return r[a]==r[b]&&r[a+l]==r[b+l];
    }
    void build_sa(int *r,int n,int m)
    {
        int i,j,p,*x=wa,*y=wb,*t;
        for(i=0;i<m;i++)we[i]=0;
        for(i=0;i<n;i++)we[x[i]=r[i]]++;
        for(i=1;i<m;i++)we[i]+=we[i-1];
        for(i=n-1;i>=0;i--)sa[--we[x[i]]]=i;
        for(j=1,p=1;p<n;j*=2,m=p){
            for(p=0,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<n;i++)wv[i]=x[y[i]];
            for(i=0;i<m;i++)we[i]=0;
            for(i=0;i<n;i++)we[wv[i]]++;
            for(i=1;i<m;i++)we[i]+=we[i-1];
            for(i=n-1;i>=0;i--)sa[--we[wv[i]]]=y[i];
            for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
        }
    }
    
    void calheight(int *r,int n)
    {
        int i,j,k=0;
        for(i=1;i<=n;i++)rk[sa[i]]=i;
        for(i=0;i<n;height[rk[i++] ]=k){
            for(k?k--:0,j=sa[rk[i]-1];r[i+k]==r[j+k];k++);
        }
    }
    
    int minx[maxn][30];
    void init_rmq(int n)
    {
        int i,j;
        height[1]=inf;
        for(i=1;i<=n;i++)minx[i][0]=height[i];
        for(j=1;j<=16;j++){
            for(i=1;i<=n;i++){
                if(i+(1<<j)-1<=n){
                    minx[i][j]=min(minx[i][j-1],minx[i+(1<<(j-1))][j-1]);
                }
            }
        }
    }
    
    int lcp(int l,int r)
    {
        int k,i;
        l=rk[l];r=rk[r];
        if(l>r)swap(l,r);
        l++;
        k=(log((r-l+1)*1.0)/log(2.0));
        return min(minx[l][k],minx[r-(1<<k)+1][k]);
    }
    
    char ss[maxn];
    int b[maxn];
    
    
    int main()
    {
        int n,m,i,j,T,l,beishu,yushu,len,k,h,cas=0;
        while(scanf("%s",ss)!=EOF)
        {
            if(strcmp(ss,"#")==0)break;
            n=strlen(ss);
            for(i=0;i<n;i++){
                a[i]=ss[i]-'a'+97+1;
            }
            a[n]=0;
            build_sa(a,n+1,130);
            calheight(a,n);
            init_rmq(n);
            int tot=0;
            int ans=-1;
            for(l=1;l<=n;l++){
                for(i=0;i+l<n;i+=l){
                    len=lcp(i,i+l );
                    beishu=len/l+1;
                    yushu=len%l;
                    if(i-(l-yushu)>=0 && lcp(i-(l-yushu),i+l-(l-yushu)  )>=len ){
                        beishu++;
                    }
                    if(ans==-1){
                        ans=beishu;
                        b[++tot]=l;
                    }
                    else if(beishu>ans){
                        tot=0;
                        b[++tot]=l;
                        ans=beishu;
                    }
                    else if(beishu==ans){
                        b[++tot]=l;
                    }
                }
            }
            int flag=0;
            for(i=1;i<=n;i++){
                for(j=1;j<=tot;j++){
                    if(lcp(sa[i],sa[i]+b[j] )>=(ans-1)*b[j]  ){   //这里不能取等号,因为并不是所有的串都是恰好匹配
                        flag=1;
                        break;
                    }
                }
                if(flag)break;
            }
    
            cas++;
            printf("Case %d: ",cas);
            for(k=1;k<=ans;k++){
                for(h=1;h<=b[j];h++){
                    printf("%c",ss[sa[i]+h-1]);
                }
            }
            printf("
    ");
        }
        return 0;
    }
    
    /*
    babbabaabaabaabab
    
    */
    


  • 相关阅读:
    C#删除程序自身【总结】
    X86(32位)与X64(64位)有什么区别,如何选择对应的操作系统和应用程序?
    【转】关于C#接口和抽象类的一些说明
    C# 的可空合并运算符(??)到底是怎样的宝宝?
    第三章 “我要点爆”微信小程序云开发之点爆方式页面和爆炸之音页面制作
    微信小程序云开发之云函数的创建与环境配置
    第五章 “我要点爆”微信小程序云开发实例之从云端获取数据制作首页
    第一章 “我要点爆”微信小程序云开发之项目建立与我的页面功能实现
    第四章 “我要点爆”微信小程序云开发之疯狂点击与糖果点爆页面制作
    Git的使用方法与GitHub项目托管方法
  • 原文地址:https://www.cnblogs.com/herumw/p/9464490.html
Copyright © 2011-2022 走看看