Longest Common prefix of suffix Array

Source: Internet
Author: User
Tags bcbc

Longest Common prefix of suffix Array
# Include
# Define maxn100
Int main ()
{
Int rank [maxn], height [maxn], sa [maxn] = {, 2}, s [maxn] = {, 3 }; // The s string can be viewed as abcbc.
Int I, j, k = 0;
For (I = 0; I <5; I ++)
Rank [sa [I] = I;
For (I = 0; I <5; I ++)
{

If (rank [I] = 0) // when rank [I] = 0, that is, height [0] does not make sense. Therefore, you can directly set height [0] = 0, k also changes to 0.
{
K = 0;
Height [0] = 0;
Continue;
}
If (k)
K --;
J = sa [rank [I]-1];
/*
When rank [k] is not equal to 0, rank [I]-1 calculates the previous ranking and uses the sa array to find the starting coordinate of the previous ranking.
*/
Printf ("% d \ n", rank [I], I, j );
While (s [I + k] = s [j + k])
K ++;
/*
The relationship between I and j and the number of characters controlled by the suffix I
The above while loop is actually when k = 0 when the completion of the sort after the suffix k and suffix I-1 adjacent (in the suffix I-1 before the suffix k ), suffix k
And suffix I-1 respectively after deleting the first character to obtain the suffix k + 1 and suffix I, so the suffix k + 1 must be in front of the suffix I (adjacent ), and the longest common prefix is the suffix k and
Suffix k and suffix I-1 longest common prefix-1 I .e. h [I-1]-1 For example:
The suffix of the string abcbc is:
0 abcbc
1 bcbc
2 cbc
3 bc
4 c
After sorting
0 abcbc
3 bc
1 bcbc
4 c
2 cbc
When I is equal to 0, rank [0], so height [0] is equal to 0, k = 0
When I is equal to 1, rank [1] is 2, rank [1]-1 = 1, in this case, we compare bcbc with the suffix bc (the subscript of the bcbc controlled by I, and the subscript of the bc controlled by j), which goes through the while () loop.
It is determined that k = 2, that is, the longest public prefix is 2.
When I is equal to 2, rank [2] is 4, rank [2]-1 = 3, in this case, we compare the cbc and the suffix c (both bcbc and bc are obtained after the two suffixes I equals 1 are removed from B,
At this time, we do not need to calculate it again. We can directly subtract 1 from the longest public prefix k of bcbc and bc without having to calculate it)
When I is equal to 3, rank [3] is 1, rank [1]-1 = 0. At this time, it is compared that bc and the suffix abcbc in front of it go through while () the longest public prefix for loop determination is 0
When I is equal to 4, rank [4] is 3, rank [4]-1 = 2. At this time, c is compared with the suffix bc that is placed before it after while () the longest public prefix for loop determination is 0
The above can be concluded that when I is from 0-n-1, so the suffix is from short to short, and when the two strings are sorted and adjacent, they all remove the previous one and are still adjacent, so we can have a long
Deliver short
*/
Height [rank [I] = k;
}
For (I = 0; I <5; I ++)
Printf ("% d", height [I]);
Puts ("");
Return 0;
}

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.