Farmer John has noticed that the quality of milk given by his cows varies from day to day. On further investigation, he discovered that although he can't predict the quality of milk from one day to the next, there are some regular patterns in the daily milk quality.
To perform a rigorous study, he has invented a complex classification scheme by which each milk sample is recorded as an integer between 0 and 1,000,000 inclusive, and has recorded data from a single cow over N (1 ≤ N ≤ 20,000) days. He wishes to find the longest pattern of samples which repeats identically at least K (2 ≤ K ≤ N) times. This may include overlapping patterns -- 1 2 3 2 3 2 3 1 repeats 2 3 2 3 twice, for example.
Help Farmer John by finding the longest repeating subsequence in the sequence of samples. It is guaranteed that at least one subsequence is repeated at least Ktimes.
Input
Lines 2.. N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.
Output
Sample Input
8 2 1 2 3 2 3 2 3 1
Sample Output
4
题解:
题目意思是让求出现次数超过k次的子串的最长长度;
思路:后缀数组,然后二分答案,求次数大于k的最长的子串;
参考代码:
1 //#include<bits/stdc++.h> 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<string> 6 #include<cstdlib> 7 #include<cmath> 8 #include<algorithm> 9 using namespace std; 10 #define clr(a,val) memset(a,val,sizeof(a)) 11 #define lowbit(x) x&-x 12 typedef long long ll; 13 const int INF=0x3f3f3f3f; 14 inline int read() 15 { 16 int x=0,f=1;char ch=getchar(); 17 while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();} 18 while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar();} 19 return x*f; 20 } 21 const int maxn=1e6+10; 22 int N,K; 23 struct SuffixArray{ 24 int s[maxn]; 25 int sa[maxn],height[maxn],rank[maxn],n; 26 int t[maxn*2],t2[maxn*2]; 27 int cnt[maxn]; 28 void build_sa(int m)//字符都属于0~m-1范围 29 { 30 int i,*x=t,*y=t2; 31 for(i=0;i<m;i++) cnt[i]=0; 32 for(i=0;i<n;i++) cnt[x[i]=s[i]]++; 33 for(i=1;i<m;i++) cnt[i]+=cnt[i-1]; 34 for(i=n-1;i>=0;i--) sa[--cnt[x[i]]]=i; 35 for(int k=1,p;k<=n;k <<=1)//k<=n 36 { 37 p=0; 38 for(i=n-k;i<n;i++) y[p++]=i; 39 for(i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k; 40 for(i=0;i<m;i++) cnt[i]=0; 41 for(i=0;i<n;i++) cnt[x[y[i]]]++; 42 for(i=1;i<m;i++) cnt[i]+=cnt[i-1]; 43 for(i=n-1;i>=0;i--) sa[--cnt[x[y[i]]]]=y[i]; 44 swap(x,y); 45 p=1;x[sa[0]]=0; 46 for(i=1;i<n;i++) 47 x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]? p-1:p++; 48 if(p>=n) break; 49 m=p; 50 } 51 } 52 void build_height() 53 { 54 int k=0; 55 for(int i=0;i<n;i++) rank[sa[i]]=i; 56 for(int i=0;i<n-1;i++) 57 { 58 if(k) k--; 59 int j=sa[rank[i]-1]; 60 while(s[i+k]==s[j+k]) k++; 61 height[rank[i]]=k; 62 } 63 } 64 } SA; 65 66 inline bool check(int ans) 67 { 68 int cnt=1; 69 for(int i=2;i<SA.n;++i) 70 { 71 if(SA.height[i]>=ans) cnt++; 72 else 73 { 74 if(cnt>=K) return true; 75 cnt=1; 76 } 77 } 78 return false; 79 } 80 81 int main() 82 { 83 N=read();K=read(); 84 for(int i=0;i<N;++i) 85 { 86 SA.s[i]=read();SA.s[i]++; 87 } 88 SA.s[N++]=0;SA.n=N; 89 SA.build_sa(maxn); 90 SA.build_height(); 91 int l=0,r=N; 92 while(l<=r) 93 { 94 int mid=l+r>>1; 95 if(check(mid)) l=mid+1; 96 else r=mid-1; 97 } 98 cout<<r<<endl; 99 return 0; 100 }