/*************************************** * ****** The subject is as follows: give a melody, which is represented by n numbers [1, 88] and calculates its longest theme length. A melody is a piece of music that appears at least twice without overlapping; repeat occurs after a piece of music is added to a certain number (for example, 1 2 3 4 5 and 5 6 7 8 9 are the same music clip, all + 4 ); the topic length must be at least 5, and the unsolvable output value is 0. algorithm analysis: If a string is given, the longest duplicate substring is obtained. The two substrings cannot overlap. The height array is obtained using a suffix array; the height array is used to divide the sorted suffixes into several groups. The height values of the Suffixes in each group are not smaller than k. However, the substrings must not overlap, therefore, the binary answer method is used. Based on the relationship between the sorted adjacent suffixes, compare whether the Distance Difference between them is greater than the answer; there is a hope that the two suffixes with the longest public prefix not less than k must be in the same group. For each group of suffixes, only the difference between the maximum and minimum sa values of each suffix must be determined. No no less than k; if a group meets the requirements, it indicates yes; otherwise, it does not; **************************************** * *****/# include <iostream> # include <cstring> # include <cstdlib> # include <cstdio> # include <climits> # include <algorithm> using namespace std; const int N = 20010; const int M = 190; // the maximum value of the r array # define F (x)/3 + (x) % 3 = 1? 0: tb) # define G (x) <tb? (X) * 3 + 1 :( (x)-tb) * 3 + 2) int wa [N], wb [N], wv [N], _ ws [M]; int c0 (int * r, int a, int B) {return r [a] = r [B] & r [a + 1] = r [B + 1] & r [a + 2] = r [B + 2];} int c12 (int k, int * r, int a, int B) {if (k = 2) return r [a] <r [B] | r [a] = r [B] & c12 (1, r, a + 1, B + 1 ); else return r [a] <r [B] | r [a] = r [B] & wv [a + 1] <wv [B + 1];} void sort (int * r, int * a, int * B, int n, int m) {for (int I = 0; I <n; I ++) wv [I] = r [a [I]; for (int I = 0; I <m; I ++) _ ws [I] = 0; for (int I = 0; I <n; I ++) _ ws [wv [I] ++; for (int I = 1; I <m; I ++) _ ws [I] + = _ ws [I-1]; for (int I = n-1; I> = 0; I --) B [-- _ ws [wv [I] = a [I]; return;} void dc3 (int * r, int * sa, int n, int m) {int I, j, * rn = r + n, * san = sa + n, ta = 0, tb = (n + 1)/3, tbc = 0, p; r [n] = r [n + 1] = 0; for (I = 0; I <n; I ++) {if (I % 3! = 0) wa [tbc ++] = I;} sort (r + 2, wa, wb, tbc, m); sort (r + 1, wb, wa, tbc, m); sort (r, wa, wb, tbc, m); for (p = 1, rn [F (wb [0])] = 0, I = 1; I <tbc; I ++) {rn [F (wb [I])] = c0 (r, wb [I-1], wb [I])? P-1: p ++;} if (p <tbc) dc3 (rn, san, tbc, p); else {for (I = 0; I <tbc; I ++) san [rn [I] = I ;}for (I = 0; I <tbc; I ++) {if (san [I] <tb) wb [ta ++] = san [I] * 3;} if (n % 3 = 1) wb [ta ++] = n-1; sort (r, wb, wa, ta, m); for (I = 0; I <tbc; I ++) wv [wb [I] = G (san [I])] = I; for (I = 0, j = 0, p = 0; I <ta & j <tbc; p ++) {sa [p] = c12 (wb [j] % 3, r, wa [I], wb [j])? Wa [I ++]: wb [j ++] ;}for (; I <ta; p ++) sa [p] = wa [I ++]; for (; j <tbc; p ++) sa [p] = wb [j ++]; return;} int rank [N], height [N]; void calheight (int * r, int * sa, int n) {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 check (int * sa, int n, int k) {int I, max = sa [1], min = sa [1]; for (I = 2; I <= n; I ++) {if (height [I] <k) max = min = sa [I]; else {if (sa [I] <min) min = sa [I]; if (sa [I]> max) max = sa [I]; if (max-min> k) return (1 );}} return (0);} int r [N * 3], sa [N * 3]; int main () {// freopen ("C: \ Users \ Administrator \ Desktop \ kd.txt "," r ", stdin); int k, n; int min, mid, max; while (scanf ("% d", & n) {int j = 0; for (int I = 0; I <n; I ++) {scanf ("% d", & k); r [I] = k-j + 100; j = k;} r [n] = 0; dc3 (r, sa, n + 1, M); calheight (r, sa, n); min = 1; max = n> 1; while (min <= max) {mid = (min + max)> 1; if (check (sa, n, mid) min = mid + 1; else max = mid-1 ;} if (max> = 4) printf ("% d \ n", max + 1); else printf ("0 \ n");} return 0 ;}