[USACO06DEC] Milk Patterns
Description
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 K times.
Input
Line 1: Two space-separated integers: N and K
Lines 2..N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.
Output
Line 1: One integer, the length of the longest pattern which occurs at least K times
Sample Input
8 2 1 2 3 2 3 2 3 1
Sample Output
4
就当是后缀数组模板题好了,真心写的心酸
二分答案,看在RMQ中所有长度为k的区间是否满足LCP大于等于mid(即二分的长度),有一个区间满足则该长度可行,然而继续二分即可。
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 template <typename T> void aout(T *a,int i,int j){ 5 for(int k=i,k_end=j;k<k_end;++k) 6 cout<<a[k]<<" ";puts(""); 7 } 8 const int N=100010; 9 int s[N]; 10 int sa[N],t[N],t2[N],c[N],n,k; 11 int Rank[N],height[N],dmin[N][20]; 12 void build_sa(int m){ 13 int i,*x=t,*y=t2; 14 for(i=0;i<m;++i)c[i]=0; 15 for(i=0;i<n;++i)++c[x[i]=s[i]]; 16 for(i=1;i<m;++i)c[i]+=c[i-1]; 17 for(i=n-1;i>=0;--i)sa[--c[x[i]]]=i; 18 for(int k=1;k<=n;k<<=1){ 19 int p=0; 20 for(i=n-k;i<n;++i)y[p++]=i; 21 for(i=0;i<n;++i)if(sa[i]>=k)y[p++]=sa[i]-k; 22 for(i=0;i<m;++i)c[i]=0; 23 for(i=0;i<n;++i)++c[x[y[i]]]; 24 for(i=1;i<m;++i)c[i]+=c[i-1]; 25 for(i=n-1;i>=0;--i)sa[--c[x[y[i]]]]=y[i]; 26 swap(x,y); 27 p=1;x[sa[0]]=0;y[n]=-1; 28 for(i=1;i<n;++i) 29 x[sa[i]]=(y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k])?p-1:p++; 30 if(p>=n)break; 31 m=p; 32 } 33 } 34 void build_height(){ 35 int i,j,k=0; 36 for(i=0;i<n;i++)Rank[sa[i]]=i; 37 for(i=0;i<n;i++) { 38 if(k)k--; 39 j=sa[Rank[i]-1]; 40 while(s[i+k]==s[j+k])k++; 41 height[Rank[i]]=k; 42 } 43 } 44 void initMin(){ 45 for(int i=1;i<=n;++i)dmin[i][0]=height[i]; 46 for(int j=1;(1<<j)<=n;++j) 47 for(int i=1;i+(1<<j)-1<=n;++i) 48 dmin[i][j]=min(dmin[i][j-1],dmin[i+(1<<(j-1))][j-1]); 49 } 50 int RMQ(int L,int R){ 51 int k=0; 52 while((1<<(k+1))<=R-L+1)k++; 53 return min(dmin[L][k],dmin[R-(1<<k)+1][k]); 54 } 55 bool check(int len){ 56 int l=0,r=l+k-2; 57 while(r<=n-1){ 58 int cnt=RMQ(l,r); 59 if(cnt>=len)return true; 60 ++l,++r; 61 } 62 return false; 63 } 64 int a[N],num[N]; 65 inline bool cmp(int __x,int __y){ return a[__x]<a[__y]; } 66 int main(){ 67 scanf("%d%d",&n,&k); 68 for(int i=0;i<n;++i)scanf("%d",&a[i]),num[i]=i; 69 sort(num,num+n,cmp); 70 s[num[0]]=1; 71 int cnt=1; 72 for(int i=0;i<n-1;++i) 73 if(a[num[i]]!=a[num[i+1]])s[num[i+1]]=++cnt; 74 else s[num[i+1]]=cnt; 75 build_sa(cnt+1); 76 build_height(); 77 initMin(); 78 int l=1,r=n,ans=0; 79 while(l<=r){ 80 int mid=l+r>>1; 81 if(check(mid))ans=mid,l=mid+1; 82 else r=mid-1; 83 } 84 printf("%d\n",ans); 85 }
据说hash可以过...干脆就来试一试吧
如果“字符个数”的范围不出问题的话,下面的程序是可以过得去吧
确定的是用hash一定是随机算法,会有一定概率被卡掉
例如:
Input
4 2 10008 0 1 1
Standard Output
1
然而下面的程序输出的是2,显然是不对的,可以发现数据里的10008是针对了随机取值K1的hash值来模糊判断的
(数据由单曦增提供)
所以这里仅仅是提供一个绝大部分条件下可行的思路
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef unsigned long long ull; 4 const int N=100010,K1=10007; 5 int s[N],Rank[N]; 6 ull K[N],H[N],Hash[N]; 7 int n,k; 8 bool cmp(const int& __x,const int& __y){ 9 return Hash[__x]<Hash[__y]||(Hash[__x]==Hash[__y]&&__x<__y); 10 } 11 bool chk(int len){ 12 int c=0; 13 for(int i=0;i<n-len+1;++i){ 14 Rank[i]=i; 15 Hash[i]=H[i]-H[i+len]*K[len]; 16 } 17 sort(Rank,Rank+n-len+1,cmp); 18 for(int i=0;i<n-len+1;++i){ 19 if(i==0||Hash[Rank[i]]!=Hash[Rank[i-1]])c=0; 20 if(++c>=k)return 1; 21 } 22 return 0; 23 } 24 int main(){ 25 scanf("%d%d",&n,&k); 26 for(int i=0;i<n;++i)scanf("%d",s+i); 27 H[n]=0;for(int i=n-1;i>=0;--i)H[i]=H[i+1]*K1+s[i]; 28 K[0]=1;for(int i=1;i<=n;++i)K[i]=K[i-1]*K1; 29 int l=1,r=n+1; 30 while(r-l>1){ 31 int m=l+r>>1; 32 if(chk(m))l=m; 33 else r=m; 34 } 35 printf("%d\n",l); 36 }