Maximum repeated substring that cannot be overlapped (pku1743)
For a given string, obtain the longest duplicate substring. The two substrings cannot overlap.
Algorithm analysis:
This question is a little more complex than the previous one. First, the second answer turns the question into a judgment question: Determine whether two substrings with K length are the same and do not overlap. The key to solving this problem is to use the height array. The sorted suffixes are divided into several groups. The height value between the suffixes of each group is not smaller than K. For example, if the string is "aabaaaab" and K is 2, the suffix is divided into four groups, as shown in Figure 5.
It is easy to see that the two suffixes with the longest public prefix not less than K must be in the same group. Then, for each group of suffixes, you only need to determine whether the difference between the maximum and minimum values of the SA values of each suffix is not less than K. If a group meets the requirements, it indicates that the Group exists. Otherwise, the group does not exist. The time complexity of the entire approach is O (nlogn ). This topic uses the height value to group suffixes.
# Define maxn 20010int wa [maxn], WB [maxn], WV [maxn], WSS [maxn]; int R [maxn], sa [maxn]; int CMP (int * r, int A, int B, int L) {return R [a] = R [B] & R [A + L] = R [B + L];}/* [multiplier algorithm O (nlgn)] Put the string to be sorted in the r array, from R [0] To R [n-1], with a length of N and the maximum value less than m before the multiplication algorithm is used, make sure that the values of the r array are greater than 0. Then add a 0 character to the original string. If the length of the original string is N, the length of the r array constructed by the suffix array should be n + 1. therefore, when calling the DA function, the corresponding N should be n + 1. */void da (int * r, int * Sa, int N, int m) {// n should be added with 1 int I, j, P, * x = wa, * Y = WB, * t; for (I = 0; I <m; I ++) WSS [I] = 0; for (I = 0; I <N; I ++) WSS [x [I] = R [I] ++; for (I = 1; I <m; I ++) WSS [I] + = WSS [I-1]; for (I = n-1; I> = 0; I --) SA [-- WSS [x [I] = I; for (j = 1, P = 1; P <n; j * = 2, M = P) {for (P = 0, I = N-J; I <N; I ++) y [p ++] = I; for (I = 0; I <n; I ++) if (SA [I]> = J) Y [p ++] = sa [I]-J; for (I = 0; I <N; I ++) WV [I] = x [Y [I]; for (I = 0; I <m; I ++) WSS [I] = 0; for (I = 0; I <n; I ++) WSS [wv [I] ++; for (I = 1; I <m; I ++) WSS [I] + = WSS [I-1]; for (I = n-1; I> = 0; I --) sa [-- WSS [wv [I] = Y [I]; for (t = x, x = Y, y = T, P = 1, X [SA [0] = 0, I = 1; I <n; I ++) x [SA [I] = CMP (Y, sa [I-1], sa [I], j )? P-1: P ++;} return;} int rank [maxn], height [maxn]; // rank [I]: the number of the I-th row; SA [I]: where is the suffix string of the column I? inverse void calheight (int * r, int * Sa, int N) for each other {// n no 1 int I, j, k = 0; for (I = 1; I <= N; I ++) rank [SA [I] = I; for (I = 0; I <N; height [rank [I ++] = k) {for (K? K --: 0, j = sa [rank [I]-1]; R [I + k] = R [J + k]; k ++ );} return;} int main () {int N; while (scanf ("% d", & N) {int I; int X, Y; scanf ("% d", & X); for (I = 0; I <n-1; I ++) {scanf ("% d", & Y ); R [I] = Y-x + 100; // no negative X = y;} cout <Endl; R [n-1] = 0; // a total of N-1 values are da (R, SA, N, 190); calheight (R, SA, n-1); for (I = 1; I <= n-1; I ++) cout