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
    
    */
    


  • 相关阅读:
    python定时任务:apscheduler的使用(还有一个celery~)
    Python定时任务-schedule vs. Celery vs. APScheduler
    结合Django+celery二次开发定时周期任务
    The Django Book 2.0--中文版
    第十二章: 部署Django
    Django扩展自定义manage命令
    使用django-extension扩展django的manage――runscript命令
    Django | 执行项目下指定的脚本
    C语言宏定义技巧——多次包括头文件内容不同
    《Java并发编程实战》第十章 避免活跃性危急 读书笔记
  • 原文地址:https://www.cnblogs.com/herumw/p/9464490.html
Copyright © 2011-2022 走看看