The suffix array is used to find the length of the longest overlapped string that can be repeated for at least K times. Of course, it can be done using hash. If the suffix array is used, check whether the number is greater than or equal to k After grouping.
# Include <cstdio> # include <cstring> # include <algorithm> # include <queue> # include <stack> # include <map> # include <set> # include <climits> # include <string> using namespace STD; # define MP make_pair # define Pb push_backtypedef long ll; typedef unsigned long ull; typedef vector <int> VI; typedef pair <int, int> PII; typedef pair <double, double> PDD; const int INF = int_max/3; const double EPS = 1e-8; con St ll linf = 1e17; const double dinf = 1e60; const int maxn = 1e6 + 10; // use the multiplication method to obtain the suffix array int wa [maxn], WB [maxn], WV [maxn], WS [maxn]; int CMP (int * r, int A, int B, int L) {return R [a] = R [B] & R [A + L] = R [B + L];} void da (int * r, int * Sa, int N, int m) {int I, j, P, * x = wa, * Y = WB, * t; for (I = 0; I <m; I ++) WS [I] = 0; for (I = 0; I <n; I ++) WS [x [I] = R [I] ++; for (I = 1; I <m; I ++) WS [I] + = ws [I- 1]; for (I = n-1; I> = 0; I --) SA [-- ws [x [I] = I; for (j = 1, P = 1; P <n; j <= 1, 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 ++) WS [I] = 0; for (I = 0; I <n; I ++) WS [wv [I] ++; for (I = 0; I <m; I ++) WS [I] + = ws [I-1]; for (I = n-1; I> = 0; I --) sa [-- ws [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 ++ ;}/// The following is the formula for solving the height array int height [maxn], rank [maxn]; 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 ++ );} int N, K, Val [maxn], sa [maxn]; bool OK (INT Len) {int nowcnt = 1; for (INT I = 1; I <= N; I ++) {If (height [I] >=len) {nowcnt ++; If (nowcnt> = k) return true;} else nowcnt = 1 ;} return false;} int main () {While (scanf ("% d", & N, & K )! = EOF) {for (INT I = 0; I <n; I ++) scanf ("% d", & Val [I]), Val [I] ++; val [N] = 0; DA (Val, SA, n + 1, 1e6 + 5); calheight (Val, SA, n); int L = 1, R = n, ans = 0; while (L <= r) {int mid = (L + r)> 1; if (OK (MID) {ans = mid; L = Mid + 1;} else r = mid-1;} printf ("% d \ n", ANS);} return 0 ;}
Poj 3261 milk patterns suffix Array