1500 suffix sort

Source: Internet
Author: User

1500 suffix Sort

time limit: 1 sspace limit: 128000 KBtopic rank: Master Master SolvingView Run ResultsTitle Description Description

Tian Kai is a freshman at MIT. Prof. Handsomeg gave him a string of n-length lowercase letters that asked him to sort the n suffixes (suffix) of the string from small to large.

What is a suffix? Suppose the string is s=s1s2 ... Sn, define Ti=sisi+1 ... Sn. T1, T2, ..., TN is called the N suffix of s.

A comparison of string sizes is defined as follows (the comparison rules are identical to those in Pascal, and students familiar with Pascal can skip this paragraph):

If a is the prefix of B, then a<b; otherwise P satisfies: a1a2 ... Ap-1=b1b2 ... BP-1,AP<>BP. If AP<BP, then a<b; otherwise a>b.

Enter a description Input Description

First line an integer n (n<=15000)

The second line is a string of length n.

Output description Output Description

The output file contains n rows, and line i is an integer pi. Indicates that all suffixes are Tp1, Tp2, ..., TPN after the small-to-large order.

Sample input Sample Input

4

Abab

Sample output Sample Output

3

1

4

2

Data range and Tips Data Size & Hint

Description: The order of suffix sorting is t3= "AB", t1= "Abab", t4= "B", t2= "Bab". So the output is 3, 1, 4, 2.

Category labels Tags Click here to expandSuffix Tree Group tree structure
//suffix array template title#include <cstdio>#include<cstring>using namespacestd;Const intn=1e5+7;intLen,maxx,sa[n],tsa[n],rank[n],trank[n],c[n];CharS[n];voidDA () {intp; Memset (c,0,sizeofc); maxx= the;  for(intI=1; i<=len;i++) c[rank[i]=s[i]]++;  for(intI=2; i<=maxx;i++) c[i]+=c[i-1];  for(inti=len;i;i--) sa[c[rank[i]]--]=i; trank[sa[1]]=p=1;  for(intI=2; i<=len;i++){        if(rank[sa[i]]!=rank[sa[i-1]]) p++; Trank[sa[i]]=Q; }     for(intI=1; i<=len;i++) rank[i]=Trank[i];  for(intk=1;p <len;k<<=1, maxx=p) {P=0;  for(inti=len-k+1; i<=len;i++) tsa[++p]=i;  for(intI=1; i<=len;i++)if(sa[i]>k) tsa[++p]=sa[i]-K; Memset (c,0,sizeofc);  for(intI=1; i<=len;i++) trank[i]=Rank[tsa[i]];  for(intI=1; i<=len;i++) c[trank[i]]++;  for(intI=2; i<=maxx;i++) c[i]+=c[i-1];  for(inti=len;i;i--) sa[c[trank[i]]--]=Tsa[i]; trank[sa[1]]=p=1;  for(intI=2; i<=len;i++){            if(rank[sa[i]]!=rank[sa[i-1]]|| rank[sa[i]+k]!=rank[sa[i-1]+K]) p++; Trank[sa[i]]=p; }         for(intI=1; i<=len;i++) rank[i]=Trank[i]; }}intMain () {scanf ("%d%s", &len,s+1);    DA ();  for(intI=1; i<=len;i++) printf ("%d\n", Sa[i]); return 0;}

1500 suffix 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.