Title 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.
outputs 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 to sample
4
Abab
Sample output Sample outputs
3
1
4
2
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.
Positive solution: Suffix array
Problem Solving Report:
Suffix array template problem
1 //It's made by jump~2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <algorithm>8#include <ctime>9#include <vector>Ten#include <queue> One#include <map> A#include <Set> - #ifdef WIN32 - #defineOT "%i64d" the #else - #defineOT "%lld" - #endif - using namespacestd; +typedefLong LongLL; - Const intMAXN =300011; + CharCH[MAXN]; A intn,m; at intWA[MAXN],WB[MAXN],WV[MAXN],SA[MAXN]; - intTONG[MAXN]; - intRANK[MAXN],HEIGHT[MAXN]; - -InlineintGetint () - { in intw=0, q=0; - CharC=GetChar (); to while((c<'0'|| C>'9') && c!='-') c=GetChar (); + if(c=='-') q=1, c=GetChar (); - while(c>='0'&& c<='9') w=w*Ten+c-'0', c=GetChar (); the returnQ? -w:w; * } $ Panax NotoginsengInlineBOOLcmpint*r,intAintBintL) {returnR[A]==R[B] && r[a+l]==r[b+L]; } - theInlinevoidda () { + int*X=WA,*Y=WB; m= the; A for(intI=0; i<m;i++) tong[i]=0; the for(intI=0; i<n;i++) tong[x[i] = (ch[i]-'a') ]++; + for(intI=1; i<m;i++) tong[i]+=tong[i-1]; - for(inti=n-1; i>=0; i--) sa[--tong[x[i]]]=i; $ intp=0;intj=1;inti; $ for(;p <n && j<n;j=j*2, m=p) { - for(p=0, i=n-j;i<n;i++) y[p++]=i; - for(i=0; i<n;i++)if(SA[I]>=J) y[p++]=sa[i]-J; the for(intI=0; i<n;i++) wv[i]=X[y[i]]; - for(intI=0; i<m;i++) tong[i]=0;Wuyi for(intI=0; i<n;i++) tong[wv[i]]++; the for(intI=1; i<m;i++) tong[i]+=tong[i-1]; - for(inti=n-1; i>=0; i--) sa[--tong[wv[i]]]=Y[i]; WuSwap (x, y); p=1; - for(x[sa[0]]=0, i=1; i<n;i++) { AboutX[SA[I]]=CMP (y,sa[i-1],sa[i],j)? p1:p + +; $ } - } - } - AInlinevoidcalheight () { + inti,j,k=0; the for(i=0; i<n;height[rank[i++]]=k) - for(k?k--:0, j=sa[rank[i]-1];ch[i+k]==ch[j+k];k++) ; $ } the theInlinevoidWork () { then=getint (); thescanf"%s", CH); - da (); in //for (int i=0;i<n;i++) rank[sa[i]]=i; the //calheight (); the for(intI=0; i<n;i++) printf ("%d\n", sa[i]+1); About } the the intMain () the { + Work (); - return 0; the}
codevs1500 suffix Ordering