Luogp3809 [TEMPLATE] suffix sorting, luogp3809 sorting
Background
This is a template question.
Description
Read a string consisting of uppercase and lowercase English letters or numbers whose length is nn. Sort all non-null suffixes of this string in lexicographically ascending order, then, output the first character of the Suffix in sequence in the original string. The location number is 11 to nn.
Input/Output Format
Input Format:
A line of strings with a length of nn that only contain uppercase and lowercase English letters or numbers.
Output Format:
A row contains n integers, indicating the answer.
Input and Output sample input sample #1: Copy
ababa
Output example #1: Copy
5 3 1 4 2
Description
N <= 10 ^ 6n <= 106
Updated the self-written suffix array.
I did not understand it for a night.
Continue tomorrow
// luogu-judger-enable-o2#include<cstring>#include<iostream>#include<cstdio>using namespace std;const int MAXN=1e6+10;int sa[MAXN],tax[MAXN],rak[MAXN],tp[MAXN],a[MAXN],N,M;char s[MAXN];int pd(int *f,int x,int y,int l){ return (f[x]==f[y]&&f[x+l]==f[y+l]);}void Q_sort(){ for(int i=0;i<=M;i++) tax[i]=0; for(int i=1;i<=N;i++) tax[rak[i]]++; for(int i=1;i<=M;i++) tax[i]+=tax[i-1]; for(int i=N;i>=1;i--) sa[ tax[rak[tp[i]]]-- ] = tp[i];}void S_sort(){ M=127; for(int i=1;i<=N;i++) rak[i]=a[i],tp[i]=i;Q_sort(); for(int w=1,p=1;p<N;w=w+w,M=p) { p=0; for(int i=1;i<=w;i++) tp[++p]=N-w+i; for(int i=1;i<=N;i++) if(sa[i]>w) tp[++p]=sa[i]-w; Q_sort();swap(tp,rak);rak[sa[1]]=p=1; for(int i=2;i<=N;i++) rak[sa[i]]=pd(tp,sa[i],sa[i-1],w)?p:++p; } for(int i=1;i<=N;i++) printf("%d ",sa[i]);}int main(){ #ifdef WIN32 freopen("a.in","r",stdin); #else #endif scanf("%s",s); N=strlen(s); for(int i=0;i<N;i++) a[i+1]=s[i]-48; S_sort(); }