POJ 1743 Musical Theme(后缀数组)

题目链接:http://poj.org/problem?id=1743

题意:找出最长的不重叠重复串。

思路:求出h数组,二分答案k,按照k将h分组。在每组中的任意两个的串都满足前缀大于等于k。求出这些串中位置的最大最小值,差大于等于k就行。





int r[N],sa[N],wa[N],wb[N],wd[N],rank[N],h[N];





int cmp(int *r,int a,int b,int L)

{

    return r[a]==r[b]&&r[a+L]==r[b+L];

}





void da(int *r,int *sa,int n,int m)

{

    int i,j,p,*x=wa,*y=wb,*t;

    FOR0(i,m) wd[i]=0;

    FOR0(i,n) wd[x[i]=r[i]]++;

    FOR1(i,m-1) wd[i]+=wd[i-1];

    FORL0(i,n-1) sa[--wd[x[i]]]=i;

    for(j=1,p=1;p<n;j<<=1,m=p)

    {

        p=0;

        FOR(i,n-j,n-1) y[p++]=i;

        FOR0(i,n) if(sa[i]>=j) y[p++]=sa[i]-j;

        FOR0(i,m) wd[i]=0;

        FOR0(i,n) wd[x[i]]++;

        FOR1(i,m-1) wd[i]+=wd[i-1];

        FORL0(i,n-1) sa[--wd[x[y[i]]]]=y[i];

        t=x;x=y;y=t;p=1;x[sa[0]]=0;

        FOR1(i,n-1) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;

    }

}







void calHeight(int *r,int *sa,int n)

{

    int i,j,k=0;

    FOR1(i,n) rank[sa[i]]=i;

    FOR0(i,n)

    {

        if(k) k--;

        j=sa[rank[i]-1];

        while(i+k<n&&j+k<n&&r[i+k]==r[j+k]) k++;

        h[rank[i]]=k;

    }

}





int n;





int OK(int x)

{

    int i=2,j,L,R,Max,Min;

    while(i<=n)

    {

        L=i;

        while(L<=n&&h[L]<x) L++;

        R=L;

        while(R+1<=n&&h[R+1]>=x) R++;

        if(R>n) return 0;

        Max=-INF,Min=INF;

        FOR(j,L,R)

        {

            Max=max(Max,max(sa[j],sa[j-1]));

            Min=min(Min,min(sa[j],sa[j-1]));

        }

        if(Max-Min>=x) return 1;

        i=R+1;

    }

    return 0;

}



void cal()

{

    int low=0,high=n,mid;

    while(low<=high)

    {

        mid=(low+high)>>1;

        if(OK(mid)) low=mid+1;

        else high=mid-1;

    }

    int ans;

    if(low<=n&&OK(low)) ans=low+1;

    else ans=high+1;

    if(ans<5) ans=0;

    PR(ans);

}



int main()

{

    while(scanf("%d",&n),n)

    {

        int i,pre,cur;

        RD(pre);

        FOR0(i,n-1) RD(cur),r[i]=cur-pre+100,pre=cur;

        n--;

        r[n]=0;

        da(r,sa,n+1,200);

        calHeight(r,sa,n);

        cal();

    }

    return 0;

}

  

你可能感兴趣的:(theme)