Title: Give a string to find the longest substring (overlapping) that has occurred over K times.
Idea: Now get out the SA array and the height array, and then the maximum value of the smallest number in the interval of the height array for each length of K. Why are so many people two points? This is just a monotone queue sweep once on the line.
CODE:
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <
Algorithm> #define MAX 1000010 using namespace std;
int len,k;
int S[max],val[max],sa[max];
int Rank[max],height[max]; inline bool Same (int x,int y,int l) {return val[x] = = Val[y] && ((x + l >= len && y + l >= len) ||
(x + L < len && y + L < len && val[x + l] = = Val[y + l]));
} void Getsuffixarray () {static int _val[max],cnt[max],q[max],lim = 1000001;
for (int i = 0; i < len; ++i) ++cnt[val[i] = s[i];
for (int i = 1; i < Lim; ++i) cnt[i] + = cnt[i-1];
for (int i = len-1; ~i; i.) sa[--cnt[val[i]] = i;
for (int d = 1;; ++d) {int top = 0,l = 1 << (d-1);
for (int i = 0; i < len; ++i) if (Sa[i] + l >= len) q[top++] = Sa[i];
for (int i = 0; i < len; ++i) if (Sa[i] >= l) q[top++] = sa[i]-l;
for (int i = 0; i < Lim; ++i) cnt[i] = 0;
for (int i = 0; i < len; ++i) ++cnt[val[q[i]];for (int i = 1; i < Len; ++i) cnt[i] + = cnt[i-1];
for (int i = len-1; ~i; i.) sa[--cnt[val[q[i]]] = q[i];
Lim = 0;
for (int i = 0,j, i < Len; ++lim) {for (j = i; j < len-1 && Same (sa[j],sa[j + 1],l); ++j);
for (; I <= J; ++i) _val[sa[i] = Lim;
} for (int i = 0; i < len; ++i) val[i] = _val[i];
if (lim = = len) break;
} return;
} void GetHeight () {for (int i = 0; i < len; ++i) rank[sa[i]] = i;
for (int k = 0,i = 0; i < len; ++i) {if (k)--k;
Int j = Sa[rank[i]-1];
while (S[i + K] = = S[j + K]) ++k;
Height[rank[i]] = k;
}} struct complex{int pos,val;
Complex (int _,int __):p OS (_), Val (__) {} Complex () {}};
int main () {cin >> len >> K;
for (int i = 0; i < len; ++i) scanf ("%d", &s[i]);
Getsuffixarray ();
GetHeight ();
Deque<complex> Q;
int ans = 0;
for (int i = 0; i < len, ++i) {while (!q.empty () && height[i] <= q.back (). val) Q.pop_back (); while (!q.empty () && i-q.Front (). POS >= k-1) Q.pop_front ();
Q.push_back (Complex (i,height[i));
ans = max (Ans,q.front (). val);
} cout << ans << endl;
return 0; }