标签:ide help ace mil ... cat break ini his
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
就当是后缀数组模板题好了,真心写的心酸
二分答案,看在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可以过...过两天试一试
标签:ide help ace mil ... cat break ini his
原文地址:http://www.cnblogs.com/ndqzhang1111/p/6903034.html