Multiplication Algorithm of suffix array and array Multiplication Algorithm
# Include <stdio. h>
# Include <string. h>
# Include <iostream>
Using namespace std;
# Define maxn123123
Char s [MAXN];
Int sa [MAXN], t [MAXN], t2 [MAXN], c [MAXN], n;
Void build (int m)
{
Int I, * x = t, * y = t2;
// In fact, the following is the count sorting
For (I = 0; I <m; I ++) c [I] = 0;
For (I = 0; I <n; I ++) c [x [I] = s [I] ++;
For (I = 0; I <m; I ++) c [I] + = c [I-1]; // In fact, at this time each number of rankings has
For (I = n-1; I> = 0; I --) sa [-- c [x [I] = I;
/* The last for loop may be inexplicably changed to the following two for loops, which may be easy to see.
For (I = n-1; I> = 0; I --)
Rank [I] = -- c [x [x [I];
For (I = n-1; I> = 0; I --)
Sa [rank [I] = I;
According to the sa [rank [I] = I theorem, rank [I] is -- c [x [x [I], so -- c [x [x [I]] to the second
The for loop is converted to the last for loop sorted by count.
*/
For (int k = 1; k <= n; k <= 1/* is equivalent to k * 2 */)
{
Int p = 0;
/* Sort the second keyword directly using the sa array, which is equivalent to a double-digit single-digit number, and y stores the second keyword.
Sa [I] value
*/
For (I = n-k; I <n; I ++)
Y [p ++] = I;/* because the second keyword is out of the range, it is regarded as less than all the second keywords, so their ranking
Rank [n-k]... rank [n-1] ranks 0, 1, 2 ,.... so y [rank [n-k]... the value of y [rank [n-1] n-k... n-1
The value of rank [0]. rank [k] is reduced to n-k... n-1 according to sa [rank [I] = I;
*/
For (I = 0; I <n; I ++)
If (sa [I]> = k) y [p ++] = sa [I]-k;
/* Because this is the second keyword in the row, when the suffix at the end may no longer have the second keyword.
In this case, all such keywords are initialized to be smaller than all the second keywords, and suffix (sa [0]) <= suffix (sa [1]) ..
When we rank the second keyword, the first keyword is useless. In other words, we cannot use the first k keywords, that is
The subscript is smaller than k, that is, sa [I] is smaller than k. For example, the sa array calculated for the first time by baba is 1 3 0 2, that is
Suffix (1) <= suffix (3) <= suffix (0) <suffix (2) that is, a <= B, and the second keyword is aba @. This @ is less than all
The first a character is useless, that is, the sa [k] = 0 is useless, because sa [k] represents a 0 first character as the lower mark
When the ranking is k, we no longer need to rank the subscripts smaller than k, but after removing the sa [] values smaller than k, their sa [] values are relative.
If 0 is removed, 132 is left. Because 1 is removed, all the values greater than or equal to 1 are reduced by 1. Similarly, if k is removed, k are subtracted.
*/
// The first keyword of base sorting x array is equivalent to rank [], and y [I] is equivalent to the first keyword. You can manually simulate baba.
For (I = 0; I <m; I ++) c [I] = 0;
For (I = 0; I <n; I ++) c [x [y [I] ++;
For (I = 0; I <m; I ++) c [I] + = c [I-1];
For (I = n-1; I> = 0; I --) sa [-- c [x [y [I] = y [I];
/*
You can replace it with the following for. More intuitively, some rank1 records the value of rank, and regard y [I] As a subscript.
For (I = 0; I <m; I ++)
Rank [I] = x [y [I];
For (I = 0; I <n; I ++) c [I] = 0;
For (I = 0; I <m; I ++)
C [rank [I] ++;
For (I = 0; I <m; I ++) c [I] + = c [I-1];
For (I = n-1; I> = 0; I --)
Rank1 [y [I] = -- c [x [y [I];
For (I = n-1; I> = 0; I --)
Sa [rank1 [y [I] = y [I];
Bringing rank1 = -- c [x [y [I] to sa [-- c [x [y [I] = y [I];
At this time, the sa array already stores the value after doubling, but at this time rank [sa [I] = I is not necessarily true, so it needs to be verified below
*/
// Calculate the new array x based on the sa and y arrays (in this case, y is the y array after the exchange of array x and array y is equivalent to the rank array)
Swap (x, y );
P = 1; x [sa [0] = 0;
For (I = 1; I <n; I ++)
X [sa [I] = y [sa [I-1] = y [sa [I] & y [sa [I-1] + k] = y [sa [I] + k]? P-1: p ++;
If (p> = n) break;
M = p;
/*
If r [sa [I-1] = r [sa [I, B must not include @ in the string of the starting l length (this is also the final
An element to @), so sa [I-1] + k, sa [I] + k are less than n-k, so here the array will not cross the border. If not
The sa [I] + k following the & Short Circuit feature won't be judged
*/
}
}
Int main ()
{
Return 0;
}
/*
Dcba
Aabb
Baba
Bababa
Bbaa
Aaab
*/