Beginner suffix Array

Source: Internet
Author: User

Suffix sort

Description

Tim is learning the data structure, and he has just learned how to compare two string sizes. That's what the book says (and the same rules as the Pascal language, and the students who have studied Pascal can skip this):
Compare the size of two different strings s1= ' p1p2p3...pn ' and s2= ' Q1Q2Q3...QM ', set n<=m.
If S1 is the prefix of S2, then s1<s2. Otherwise set Pi<>qi, and I the smallest, if pi<qi, then s1<s2, otherwise s1>s2.
Tim wanted to practice this rule skillfully by practicing, so he was going to make a lot of strings and sort them from small to large. But Tim is so lazy that it's too much trouble to write a random K-long string. But the smart guy came up with a good idea, he wrote a very long string, and said to himself, "I just want all the suffixes of this string to be sorted from small to large."

Input

There is only one row in the input file suffix.in, and it is a string containing only lowercase letters, and the length k does not exceed 10^5.

Output

There are k lines, one number per line, and the number pi for line I indicates that all suffixes, and I small is the suffix that is guided by the original string, the pi character.

Sample Input

Mississippi

Sample Output

1185211097463

Source

Oibh (suffix array)

Suffix array: The suffix array sa is a one-dimensional array that holds 1. An arrangement of N sa[1],sa[2],......,sa[n], and guaranteed Suffix (Sa[i]) < Suffix (sa[i+1]), 1≤i<n.

That is, the n suffixes of s are sorted from small to large, and the first position of the ordered suffix is placed in the SA.

Rank array: Rank array rank[i] saved is suffix (i) in all suffixes from small to large row of "rank".

The main idea of the multiplication algorithm for suffix arrays is to use the multiplication method to start each character with a length of 2k.
The rank value is calculated by sorting the string. K starting from 0, add 1 each time, when 2k is greater than N to
, each character starts with a substring of 2k in length equal to all suffixes. And these sub-words
Character string must have been compared to the size, that is, the rank value does not have the same value, then the rank value at this time
is the final result. Each order takes advantage of the rank value of the last string of length 2k-1, so long
A string of 2k can be represented as a keyword with two 2k-1-length strings, but
The rank value of a string of length 2k is obtained after the cardinality is sorted. With the string "Aabaaaab"
As an example, the entire procedure is shown in 2. where x and Y are two keywords representing a string of length 2k.

#include <cstdio> #include <iostream> #include <cmath> #include <cstring> #define MAXN 100010using namespace Std;char s[maxn];int n,m,tot,sum[maxn],t1[maxn],t2[maxn],rank[maxn],sa[maxn];void Get_SA () { int *x=t1,*y=t2;for (int i=1;i<=n;i++) sum[x[i]=s[i]]++;for (int i=1;i<=255;i++) sum[i]+=sum[i-1];for (int i=1;i& lt;=n;i++) sa[sum[x[i]]--]=i;tot=0;for (int len=1;tot<n;len<<=1,m=tot) {tot=0;for (int i=n-len+1;i<=n;i+ +) y[++tot]=i;for (int i=1;i<=n;i++) if (Sa[i]>len) y[++tot]=sa[i]-len;for (int i=1;i<=m;i++) sum[i]=0;for (int i=1;i<=n;i++) sum[x[y[i]]]++;for (int i=1;i<=m;i++) sum[i]+=sum[i-1];for (int i=n;i>=1;i--) SA[sum[x[y[i]]]- -]=y[i];swap (x, y); x[sa[1]]=tot=1;for (int i=2;i<=n;i++) {if (y[sa[i]]!=y[sa[i-1]]| | Y[sa[i]+len]!=y[sa[i-1]+len]) Tot++;x[sa[i]]=tot;}} for (int i=1;i<=n;i++) rank[i]=x[i];} int main () {scanf ("%s", s+1), N=strlen (s+1), M=123;get_sa (), for (int i=1;i<=n;i++) printf ("%d\n", Sa[i]); return 0;}

Beginner suffix Array

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.