uoj#35 suffix Ordering

Source: Internet
Author: User

This is a template problem.

Read into a string of n -Length lowercase English letters, order 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. The position number is 1 to n.

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

Input format

A single line of n -length strings containing only lowercase English letters.

Output format

The first row n integers, and the I integer represents the position of the first character in the original string for the suffix of i .

Second linen−1 An integer, section i integers represent the length of the longest public prefix that is ranked i and the suffix ranked i+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
Restrictions and conventions

1≤n≤5

time limit :1s

space Limit : Up toMB

My suffix balance tree is finally over.

#include <cstdio>#include<cctype>#include<queue>#include<ctime>#include<cstring>#include<algorithm>#defineMid l+r>>1#defineRep (s,t) for (int i=s;i<=t;i++)#defineren for (int i=first[x];i!=-1;i=next[i])using namespaceStd;inlineintRead () {intx=0, f=1;CharC=GetChar ();  for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1;  for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; returnx*F;}Const intmaxn=100010; typedefLong Longll;inth[maxn],ch[maxn][2],c[maxn],tot,rt;ll RK[MAXN];voidRebuildinto,ll l,ll R) {    if(!o)return; rk[o]=l+R; Rebuild (ch[o][0],L,MID); Rebuild (ch[o][1],mid,r);}voidRotateint& O,intd,ll l,ll R) {    intk=ch[o][d^1];ch[o][d^1]=ch[k][d];ch[k][d]=o;o=K; Rebuild (O,l,r);}intcmpintXintY) {returnc[x]<c[y]| | (c[x]==c[y]&&rk[x-1]<rk[y-1]);}voidInsertint&o,ll l,ll R) {    if(!o) {o=tot;rk[o]=l+r;return;} if(CMP (TOT,O)) {Insert (ch[o][0],L,MID);if(h[ch[o][0]]>h[o]) rotate (o,1, l,r);} Else{Insert (ch[o][1],MID,R);if(h[ch[o][1]]>h[o]) rotate (o,0, L,r);}}voidInsertintx) {c[++tot]=x;h[tot]=rand () *rand (); Insert (RT,1,1ll<< A);}voidPrintinto) {if(!o)return; Print (ch[o][0]); printf ("%lld", Rk[o]); Print (ch[o][1]);}intN,SA[MAXN],RANK[MAXN],HEIGHT[MAXN];intCMP2 (intAintb) {returnrk[n-a+1]<rk[n-b+1];}CharS[MAXN];intMain () {Srand (0)); scanf ("%s", s); n=strlen (s); Rep (1, n) Insert (s[n-i]-'a'+1), sa[i]=i; Sort (SA+1, sa+n+1, CMP2); Rep (1, N) rank[sa[i]]=i,printf ("%d", Sa[i]);p UTS ("");  for(intI=1, j,k=0; i<=n;i++) {J=sa[rank[i]-1];  while(s[i+k-1]==s[j+k-1]) k++; Height[rank[i]]=k?k--: K; } Rep (2, N) printf ("%d", Height[i]); return 0;}
View Code

uoj#35 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.