UOJ35 suffix Array (template)

Source: Internet
Author: User

#35. Suffix sort

This is a template problem.

Read in a length ofNn  a string of lowercase English letters, please sort all non-null suffixes of the string from small to large in the dictionary order, and then sequentially output the position of the first character of the suffix in the original string. Location number is   1 "> 1 1  to   n "> n n.

In addition to further proving that you do have the ability to sort suffixes, please also output n−1n−1 integers representing the length of the longest common prefix of the neighboring suffix after sorting.

Input format

A line of n-n characters that contain only lowercase English letters.

Output format

First lineNn integers, and I -i integers represent the position of the first character in the original string that is ranked as the suffix of i i.

Second linen−1 n−1 an integer, sectionIThe I integer represents the length of the longest public prefix that is ranked as I I and is ranked as the suffix of the i+1i+1.

Sample One input
Ababa
Output
5 3 1 4 21 3 0 2
Explanation

After sorting the result is:

    1. a
    2. aba
    3. ababa
    4. ba
    5. baba

Positive solution: Suffix array

Problem Solving Report:
Template, the first time to find my original template is a withered, too scary ...

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,tot; at intWA[MAXN],WB[MAXN],WV[MAXN],C[MAXN]; - intRANK[MAXN],HEIGHT[MAXN]; - intSA[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 NotoginsengInlinevoidDaintm) { -     inti,*x=wa,*y=WB; the      for(i=1; i<=m;i++) c[i]=0; +      for(i=1; i<=n;i++) c[x[i]= (ch[i]-'a'+1)]++; A      for(i=1; i<=m;i++) c[i]+=c[i-1]; the      for(i=n;i>=1; i--) sa[c[x[i]]--]=i; +      for(intk=1, p;k<=n;k=k*2) { -p=0; $      for(i=n-k+1; i<=n;i++) y[++p]=i; $      for(i=1; i<=n;i++)if(sa[i]>k) y[++p]=sa[i]-K; -      for(i=1; i<=m;i++) c[i]=0; -      for(i=1; i<=n;i++) c[x[y[i]]]++;  the      for(i=1; i<=m;i++) c[i]+=c[i-1]; -      for(i=n;i>=1; i--) sa[c[x[y[i]]]--]=Y[i];WuyiSwap (x, y); x[sa[1]]=1; p=1; the      for(i=2; i<=n;i++) x[sa[i]]= (y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+k]==y[sa[i]+k])? p:++p; -     if(p==n) Break; m=p; Wu     } - } About  $Inlinevoidcalheight () { -     inti,j,k=0; -      for(i=1; i<=n;height[rank[i++]]=k) -      for(k?k--:0, j=sa[rank[i]-1];ch[i+k]==ch[j+k];k++) ; A } +  theInlinevoidWork () { -scanf"%s", ch+1); N=strlen (ch+1); $Da the); the      for(intI=1; i<=n;i++) rank[sa[i]]=i; the calheight (); the      for(intI=1; i<=n;i++) printf ("%d", Sa[i]); theprintf"\ n"); -      for(intI=2; i<=n;i++) printf ("%d", Height[i]); in } the  the intMain () About { the Work (); the   return 0; the}

UOJ35 suffix Array (template)

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.