[tyvj1860] Suffix array

Source: Internet
Author: User

P1860 suffix array

                time: 1000ms/space: 131072kib/java class Name: main             description                           we define a string with the suffix suffix (i) representing the substring from s[i] to S[length (s)]. The suffix Array (suffix array) Sa[i] holds an arrangement that satisfies Suffix (Sa[i]) <suffix (sa[i+1])   Compares definitions by dictionary order Height[i] means Suffix (sa[i ]) is the longest common prefix length between suffix (sa[i-1]), where height[1]=0 your task is to find the two arrays of SA and height. String length <=200000                      Input Formats                          a line that is a string in the description (only lowercase letters appear)                       output format                          a total of two lines, n number per row, first behavior Sa[i], second behavior height[i], where each line is separated by a space                       test Sample 1 input
Aabaaaab
Output
4 5 6 1 7 2 8 3 0 3 2 3 1 2 0 1

Suffix array template

#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMAXN =200000+Ten;intN, M;CharS[MAXN];intSA[MAXN], RANK[MAXN], HEIGHT[MAXN], TAX[MAXN], Tp[maxn];inlineBOOLcmpint*arr,intXintYintl) {    returnARR[X] = = Arr[y] && arr[x + l] = = Arr[y +l];} InlinevoidRsort () { for(inti =0; I <= m; i++) Tax[i] =0;  for(inti =1; I <= N; i++) tax[rank[tp[i]]]++;  for(inti =1; I <= m; i++) Tax[i] + = tax[i-1];  for(inti = n; I i--) sa[tax[rank[tp[i]]]--] =tp[i];}voidsuffix () {m=127;  for(inti =1; I <= N; i++) {Rank[i]=S[i]; Tp[i]=i;    } rsort ();  for(intW =1, p; W < N; m = p, W <<=1) {p=0;  for(inti = N-w +1; I <= N; i++) Tp[++p] =i;  for(inti =1; I <= N; i++)if(Sa[i] > W) tp[++p] = Sa[i]-W;        Rsort ();        Swap (rank, TP); P= rank[sa[1]] =1;  for(inti =2; I <= N; i++) Rank[sa[i]]= CMP (TP, Sa[i-1], Sa[i], W)? P: + +p; }     for(inti =1, J, k =0; I <= N; height[rank[i++]] =k) for(K. k--:0, j = Sa[rank[i]-1]; S[i + K] = = S[j + K]; k++);}intMain () {scanf ("%s", S +1); N= strlen (S +1);    Suffix ();  for(inti =1; I <= N; i++) printf ("%d", Sa[i]); Puts"");  for(inti =1; I <= N; i++) printf ("%d", Height[i]); return 0;}

[tyvj1860] 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.