Luogp3809 [TEMPLATE] suffix sorting, luogp3809 sorting

Source: Internet
Author: User

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();    }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.