标签:int cpp aaa pre new swa [] har build
例如:输入s[]="aaa",输出sa[0]=2,sa[1]=1,sa[2]=0;
void build_sa(const char s[],int sa[])
{
int m=128,n=strlen(s);
int *x=new int[n],*y=new int[n],*c=new int[128];
for (int i=0;i<m;++i)c[i]=0;
for (int i=0;i<n;++i) c[x[i]=s[i]]++;
for (int i=1;i<m;++i) c[i]+=c[i-1];
for (int i=n-1;i>=0;--i) sa[--c[x[i]]]=i;
for (int k=1;k<=n;k<<=1)
{
int p=0;
for (int i=n-1;i>=n-k;--i) y[p++]=i;
for (int i=0;i<n;++i) if(sa[i]>=k) y[p++]=sa[i]-k;
for (int i=0;i<m;++i) c[i]=0;
for (int i=0;i<n;++i) c[x[i]]++;
for (int i=1;i<m;++i) c[i]+=c[i-1];
for (int i=n-1;i>=0;--i) sa[--c[x[y[i]]]]=y[i];
swap (x,y);
p=1,x[sa[0]]=0;
for(int i=1;i<n;++i)x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
m=p;
}
delete[] x,y,c;
}
标签:int cpp aaa pre new swa [] har build
原文地址:https://www.cnblogs.com/maoruimas/p/9767930.html