Suffix array tyvj P1860 suffix array

Source: Internet
Author: User

/*p1860 suffix Array
Time: 1000ms/Space: 131072kib/java class Name: Main
Describe

We define a string with a suffix of 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]) compared in dictionary order
Defines Height[i] represents the longest common prefix length between suffix (sa[i]) and suffix (sa[i-1]), where height[1]=0
Your task is to find the two arrays of SA and height. String length <=200000
Input format

A line, which is a string in the description (only lowercase letters will 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*/
The topic algorithm is the title name, but also is the most simple suffix array application.
About suffix arrays I don't want to say more, recommended algorithm collection of "suffix array-a powerful tool for handling strings"
Sometimes I really want someone to tell me that self-study is very, very painful.
But 2 points timed out, but no matter what.
#include <cstdio>
#include <iostream>
#include <cstring>
#define N 200008
using namespace Std;
int n,sa[2][n],rk[2][n],a[n],v[n],k,h[n];
Char Ch[n];
void Geng (int sa[n],int rk[n],int sa[n],int rk[n])
{
for (int i=1;i<=n;i++)
V[rk[sa[i]]]=i;
for (int i=n;i;i--)
if (sa[i]>k)
Sa[v[rk[sa[i]-k]]--]=sa[i]-k;
for (int i=n-k+1;i<=n;i++)
Sa[v[rk[i]]--]=i;
for (int i=1;i<=n;i++)
rk[sa[i]]=rk[sa[i-1]]+ (rk[sa[i]]!=rk[sa[i-1]]| | Rk[sa[i]+k]!=rk[sa[i-1]+k]);
Return
}
int main ()
{
Gets (ch+1);
N=strlen (ch+1);
for (int i=1;i<=n;i++)
a[i]=ch[i]-' a ' +1;
int p=0,q=1;
for (int i=1;i<=n;i++)
v[a[i]]++;
for (int i=2;i<27;i++)
V[I]+=V[I-1];
for (int i=1;i<=n;i++)
Sa[p][v[a[i]]--]=i;
for (int i=1;i<=n;i++)
rk[p][sa[p][i]]=rk[p][sa[p][i-1]]+ (A[sa[p][i]]!=a[sa[p][i-1]);
for (K=1;k<n;k<<=1)
{
Geng (Sa[p],rk[p],sa[q],rk[q]);
Swap (P,Q);
}
for (int i=1;i<=n;i++)
printf ("%d", sa[p][i]);
printf ("\ n");
k=0;
for (int i=1;i<=n;i++)
if (rk[p][i]==1)
h[1]=0;
Else
{
int j=sa[p][rk[p][i]-1];
for (; a[i+k]==a[j+k];k++);
H[rk[p][i]]=k;
if (k>0)
k--;
}
for (int i=1;i<=n;i++)
printf ("%d", h[i]);
return 0;
}

Suffix array tyvj P1860 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.