【tyvj 1860】 后缀数组模板

题目链接:
www.tyvj.cn/p/1860

后缀数组讲解:
http://www.cnblogs.com/staginner/archive/2012/02/02/2335600.html

http://blog.csdn.net/speknighter/article/details/46640121

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#define maxn (200005)
using namespace std;
int SA[maxn],WS[maxn],wv[maxn],wa[maxn],wb[maxn],R[maxn];
char s[maxn];
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 *x=wa,*y=wb,*t,i,j,p;
    for (i=0;i<m;i++) WS[i]=0;
    for (i=0;i<n;i++) WS[x[i]=r[i]]++;
    for (i=1;i<m;i++) WS[i]+=WS[i-1];
    for (i=n-1;i>=0;i--) sa[--WS[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>=0) y[p++]=sa[i]-j;
        for (i=0;i<n;i++) wv[i]=x[y[i]];
        for (i=0;i<m;i++) WS[i]=0;
        for (i=0;i<n;i++) WS[wv[i]]++;
        for (i=1;i<m;i++) WS[i]+=WS[i-1];
        for (i=n-1;i>=0;i--) sa[--WS[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++;
    }
    return ;
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
    int i,j,k=0;
    for (i=1;i<=n;i++) rank[sa[i]]=i;
    for (i=0;i<n;height[rank[i++]]=k)
    for (k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
    return ;
}
int main()
{
    scanf("%s",s);
    int len=strlen(s);
    for (int i=0;i<len;i++)
    R[i]=s[i]-'a'+1;
    R[len]=0;
    da(R,SA,len+1,28);
    calheight(R,SA,len);
    for (int i=1;i<=len;i++)
    printf("%d ",SA[i]+1);
    printf("\n");
    for (int i=1;i<=len;i++)
    printf("%d ",height[i]);
}

你可能感兴趣的:(后缀数组)