1717: [Usaco2006 dec]milk Patterns mode of milk productionTime Limit:5 Sec Memory limit:64 MB submit:1064 solved:580 [Submit][status][discuss]
The following:Suffix array to find the height array, and then find the adjacent K-string LCP can, do not need to let the length of the string is greater than k, because equal to K is better than greater than k, monotonous queue maintenance continuous k-1 height of the minimum value (because k-1 height equivalent to K of the string of LCP).
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> const int n=200010
;
using namespace Std;
int n,k;
int M,p,wa[n],wb[n],sa[n],rk[n],he[n],c[n],cnt,q[n],ans;
Char S[n];
void Getsa () {int *x=wa,*y=wb;
M=255,p=0;
for (int i=1;i<=n;i++) c[x[i]=s[i]]++;
for (int i=1;i<=m;i++) c[i]+=c[i-1];
for (int i=n;i>=1;i--) sa[c[x[i]]--]=i;
for (int k=1;p<n;k<<=1) {p=0;
for (int i=n-k+1;i<=n;i++) y[++p]=i;
for (int i=1;i<=n;i++) if (sa[i]>k) y[++p]=sa[i]-k;
Memset (C,0,sizeof (c));
for (int i=1;i<=n;i++) c[x[y[i]]]++;
for (int i=1;i<=m;i++) c[i]+=c[i-1];
for (int i=n;i>=1;i--) sa[c[x[y[i]]]--]=y[i];
Swap (x, y);
P=1;
X[sa[1]]=p; for (int i=2;i<=n;i++) {if (y[sa[i]]!=y[sa[i-1]]| |
Y[SA[I]+K]!=Y[SA[I-1]+K]) p++;
X[sa[i]]=p;
} m=p;
}} void Getrk () {for (int i=1;i<=n;i++) rk[sa[i]]=i;} void Gethe () {int j=0;
for (int i=1;i<=n;i++) {if (rk[i]==1) continue; while (s[i+j]==s[sA[RK[I]-1]+J]) J + +;
He[rk[i]]=j;
if (j) j--;
}} int main () {scanf ("%d%d", &n,&k);
for (int i=1;i<=n;i++) scanf ("%d", &s[i]);
Getsa (), GETRK (), gethe ();
int h=1,t=0;
k--;
for (int i=1;i<=n;i++) {while (h<=t&&q[h]<=i-k) h++;
while (H<=t&&he[i]
Description
Farmer John found that the quality of his cow's milk had been changing. After careful investigation, he found that although he could not foresee the quality of milk produced tomorrow, there was a lot of overlap in the quality of several consecutive days. We call it a "pattern". John's milk by mass can be given a number between 0 and 1000000. And John recorded the milk quality value for N (1<=n<=20000) days. He wanted to know the length of the longest pattern that appeared at least K (2<=k<=n) times. For example 1 2 3 2 3 2 3 1 medium 2 3 2 3 appeared two times. When k=2, this length is 4. Input
* Line 1: two integer n,k.
* Lines 2..n+1: An integer per line represents the mass value of the day. Output
* Line 1: An integer: The length of the longest occurrence of a pattern of at least k times in n days Sample Input 8 2
1
2
3
2
3
2
3
1
Sample Output 4
HINT
Source
Gold [Submit][status][discuss]