-
First things first
这玩意似乎我17年搞过,但是现在毫无记忆qwq
用处是对后缀排序,和求LCS
-
正文
倍增法很好理解,运用基数排序,每次排序一倍长度的前缀
qwq
这点东西背板子比较好
详细解释留坑
(update on 7.18.2019)
New Code!
/*
@Date : 2019-07-18 16:46:50
@Author : Adscn (adscn@qq.com)
@Link : https://www.cnblogs.com/LLCSBlog
*/
#include<bits/stdc++.h>
using namespace std;
#define IL inline
#define RG register
#define gi getint()
#define gc getchar()
#define File(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
IL int getint()
{
RG int xi=0;
RG char ch=gc;
bool f=0;
while(ch<'0'|ch>'9')ch=='-'?f=1:f,ch=gc;
while(ch>='0'&ch<='9')xi=(xi<<1)+(xi<<3)+ch-48,ch=gc;
return f?-xi:xi;
}
template<typename T>
IL void pi(T k,char ch=0)
{
if(k<0)k=-k,putchar('-');
if(k>=10)pi(k/10,0);
putchar(k%10+'0');
if(ch)putchar(ch);
}
const int N=1e7+7;
namespace SuffixArray{
static char s[N];
static int sa[N],rkx[N],rky[N],n,m,*key1,*key2,rank[N];
int cnt[300];
void In(){scanf("%s",s+1);n=strlen(s+1);}
inline void Qsort()
{
memset(cnt,0,sizeof(int)*m+sizeof(int)*7);
for(int i=1;i<=n;++i)++cnt[key1[i]];
for(int i=1;i<=m;++i)cnt[i]+=cnt[i-1];
for(int i=n;i;--i)sa[cnt[key1[key2[i]]]--]=key2[i];
}
void Get()
{
m=150,key1=rkx,key2=rky;
for(int i=1;i<=n;++i)
key1[i]=s[i],key2[i]=i;
Qsort();
for(int k=1,p=0;p<n;k<<=1)
{
p=0;
for(int i=n-k+1;i<=n;++i)key2[++p]=i;//this key2 mean the second key 's address not rank
for(int i=1;i<=n;++i)if(sa[i]>k)key2[++p]=sa[i]-k;
Qsort();
swap(key1,key2);
p=key1[sa[1]]=1;
for(int i=2;i<=n;++i)
key1[sa[i]]=(
key2[sa[i-1]]==key2[sa[i]]&&
key2[sa[i-1]+k]==key2[sa[i]+k])?p:++p;
m=p;
}
for(int i=1;i<=n;i++)rank[sa[i]]=i;
}
void getheight()
{
int k=0;
for(int i=1;i<=n;i++)
{
if(k)--k;
int p=sa[rank[i]-1];
while(s[i+k]==s[p+k])++k;
height[rank[i]]=k;
}
}
void print()
{
for(int i=1;i<=n;++i)pi(sa[i],' ');
}
}
int main(void)
{
SuffixArray::In();
SuffixArray::Get();
SuffixArray::print();
return 0;
}