poj 1743 Musical Theme 后缀数组da算法

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;

const int maxn=200000;
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 rank1[],int height[],int n,int m)
{
    n++;
    int i,j,p,*x=t1,*y=t2;
    //第一轮基数排序,如果s的最大值很大,可改为快速排序
    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;
        //直接利用sa数组排序第二关键字
        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;
        //这样数组y保存的就是按照第二关键字排序的结果
        //基数排序第一关键字
        for(i=0; i<m; i++) c[i]=0; //后面的j个数第二关键字为空的最小
        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];
        //根据sa和x数组计算新的x数组
        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++)
        rank1[sa[i]]=i;
    for(i=0; i<n; i++)
    {
        if(k) k--;
        j=sa[rank1[i]-1];
        while(str[i+k]==str[j+k]) k++;
        height[rank1[i]]=k;
    }
}
int rk[maxn],height[maxn],str[maxn],sa[maxn];

/*int check(int mid,int n)
{
    int st,ed,i;
    for(i=2; i<=n; i++)
    {
        st=ed=sa[i-1];
        while(height[i]>=mid)
        {
            if(sa[i]<st) st=sa[i];
            else if(sa[i]>ed) ed=sa[i];
            i++;
        }
        if(ed-st>mid) return 1;
    }
    return 0;
}*/
int check(int mid,int n)
{
    int st=sa[0],ed=sa[0],i;
    for(i=0; i<=n; i++)
    {
        if(height[i]>=mid)
        {
            st=min(st,sa[i]);
            ed=max(ed,sa[i]);
            //printf("%d %d\n",st,ed);
            if(ed-st>mid) return 1;
        }
        else
        {
            st=sa[i];
            ed=sa[i];
        }

    }
    return 0;
}

int main()
{
    int i,j,n,m,l,r,mid,ans;
    while(~scanf("%d",&n))
    {
        if(n==0) break;
        for(i=0; i<n; i++)
            scanf("%d",&str[i]);
        if(n<=9)
        {
            printf("0\n");
            continue;
        }
        for(i=1; i<n; i++)
        {
            str[i-1]=str[i]-str[i-1]+88;
        }
        str[n-1]=0;
        //for(i=0;i<n;i++) printf("%d %d\n",i,str[i]);

        da(str,sa,rk,height,n,200);
        //for(i=0;i<n;i++)
           // printf("%d %d %d\n",i,str[i],height[i]);
        l=0;
        r=n;
        ans=0;
        while(l<=r)
        {
            mid=l+(r-l)/2;
            if(check(mid,n))
            {
                l=mid+1;
                ans=mid;
            }
            else
                r=mid-1;
        }
        ans+=1;
        if(ans<5) ans=0;
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值