2251: [2010Beijing Wc]外星联络 后缀数组

WC的题?
后缀数组求出来以暴力统计就好了= =

#include<bits/stdc++.h>
using namespace std;
int len;
int cc[3005],t1[3005],t2[3005],sa[3005],rank[3005],height[3005];;
char s[3005];
inline char read()
{
    char c=getchar();
    while (c<'A'||c>'Z') c=getchar();
    return c;
}
inline bool cmp(int *y,int a,int b,int l)
{
    int arank1=y[a];
    int brank1=y[b];
    int arank2=a+l>=len?-1:y[a+l];
    int brank2=b+l>=len?-1:y[b+l];
    return arank1==brank1&&arank2==brank2;
}
inline void make_sa()
{
    int *x=t1,*y=t2;
    int m=256;
    for (int i=0;i<m;i++) cc[i]=0;
    for (int i=0;i<len;i++) ++cc[x[i]=s[i]];
    for (int i=1;i<m;i++) cc[i]+=cc[i-1];
    for (int i=len-1;i>=0;i--) sa[--cc[x[i]]]=i;
    for (int k=1;k<len;k<<=1)
    {
        int p=0;
        for (int i=len-k;i<len;i++) y[p++]=i;
        for (int i=0;i<len;i++)
            if (sa[i]>=k) y[p++]=sa[i]-k;
        for (int i=0;i<m;i++) cc[i]=0;
        for (int i=0;i<len;i++) ++cc[x[y[i]]];
        for (int i=1;i<m;i++) cc[i]+=cc[i-1];
        for (int i=len-1;i>=0;i--) sa[--cc[x[y[i]]]]=y[i];
        swap(x,y);
        m=1; x[sa[0]]=0;
        for (int i=1;i<len;i++)
            x[sa[i]]=cmp(y,sa[i-1],sa[i],k)?m-1:m++;
        if (m>=len) break;
    }
}       
inline void make_height()
{
    for (int i=0;i<len;i++) rank[sa[i]]=i;
    int k=0; height[0]=0;
    for (int i=0;i<len;i++)
    {
        if (!rank[i]) continue;
        if (k) k--;
        int j=sa[rank[i]-1];
        while (s[j+k]==s[i+k]) k++;
        height[rank[i]]=k;
    }
}
int main()
{
    scanf("%d",&len);
    scanf("%s",s);
    make_sa();
    make_height();
    for (int i=0;i<len;i++)
        for (int j=height[i]+1;sa[i]+j-1<len;j++)
        {
            int l,r;
            for (l=i;height[l]>=j&&l>=0;l--);
            for (r=i+1;height[r]>=j&&r<len;r++);
            if (r-l>1) printf("%d\n",r-l);
        }
    return 0;
}

你可能感兴趣的:(2251: [2010Beijing Wc]外星联络 后缀数组)