Postfix array + dichotomy answer + discretization. (last time I wrote, I saw that the data was small and not discretized and then wa ... Wrote the LSJ brother's writing.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace STD; #define REP (I,s,t) for (int. i=s;i<=t;i++) #define DWN (i,s,t) for (int i=s;i>=t;i--) #define CLR (x,c) memset (X,c, sizeof (x)) int read () {int X=0;char C=getchar (), while (!isdigit (c)) C=getchar (), while (IsDigit (c)) x=x*10+c-' 0 ', c= GetChar (); return x;} const int Nmax=20005;const int inf=0x7f7f7f7f;struct node{int id[nmax],n;node () {n=0;} inline void Add (int x) {id[n++]=x;} inline void work () {sort (id,id+n); N=unique (Id,id+n)-id;} inline int hash (int x) {return lower_bound (id,id+n,x)-id;}} H;int sa[nmax],t[nmax],t2[nmax],c[nmax],rank[nmax],height[nmax],s[nmax],n,k;void Build_sa () {int m=h.N,*x=t,*y=t2; for (int i=0;i<m;i++) c[i]=0;for (int i=0;i<n;i++) c[x[i]=s[i]]++;for (int i=1;i<m;i++) c[i]+=c[i-1];for (int i= n-1;i>=0;i--) sa[--c[x[i]]]=i;for (int k=1;k<=n;k<<=1) {int p=0;for (int i=n-k;i<n;i++) y[p++]=i;for ( int i=0;i<n;i++) if (sa[i]>=k) y[p++]=sa[i]-k;for (int i=0;i< m;i++) c[i]=0;for (int i=0;i<n;i++) c[x[i]]++;for (int i=1;i<m;i++) c[i]+=c[i-1];for (int i=n-1;i>=0;i--) sa[- -c[x[y[i]]]]=y[i];swap (x, y);p =1;x[sa[0]]=0;for (int i=1;i<n;i++) x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[ I-1]+k]==y[sa[i]+k]?p-1:p++;if (P>=n) break;m=p;}} void Build_height () {int k=0;for (int i=0;i<n;i++) rank[sa[i]]=i;for (int i=0;i<n;i++) {if (k) K--;int j=sa[rank[i]- 1];while (S[i+k]==s[j+k]) k++;height[rank[i]]=k;} BOOL Check (int x) {int cnt=1;for (int i=1;i<n;i++) {if (height[i]>=x) {cnt++;if (cnt>=k) return true;} else cnt=1;} return false;} int main () {N=read (), K=read (); REP (i,0,n-1) S[i]=read (), H.add (S[i]); H.add (S[n++]=-inf); H.work (); REP (i,0,n-1) S[i]=h.hash (S[i]); Build_sa (); build_height (); int L=0,r=n,ans=0,mid;while (l<=r) {mid= (l+r) >>1 ; if (check (mid)) Ans=mid,l=mid+1;else R=mid-1;} printf ("%d\n", ans); return 0;}
1717: [Usaco2006 dec]milk Patterns Milk-producing mode time limit:5 Sec Memory limit:64 MB
submit:948 solved:516
[Submit] [Status] [Discuss] 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 Input8 2
1
2
3
2
3
2
3
1
Sample Output4
HINT Source
Gold
[Submit] [Status] [Discuss]
bzoj1717: [Usaco2006 dec]milk Patterns mode of milk production