POJ3261(后缀数组+2分枚举)
2016-03-01 20:58
260 查看
Milk Patterns
DescriptionFarmer 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.InputLine 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.
OutputLine 1: One integer, the length of the longest pattern which occurs at least K times
Sample Input
Sample Output
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 12972 | Accepted: 5769 | |
Case Time Limit: 2000MS |
Lines 2..N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.
OutputLine 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 思路:用后缀数组求出lcp后,2分枚举L使得连续的lcp[i]>=L 的个数>=k-1;
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN=1000005; int buf[MAXN]; int sa[MAXN]; int rnk[MAXN]; int tmp[MAXN]; int lcp[MAXN]; int len,k; int t; bool comp(int i,int j) { if(rnk[i]!=rnk[j]) return rnk[i]<rnk[j]; else { int ri=(i+k<=len)?rnk[i+k]:-1; int rj=(j+k<=len)?rnk[j+k]:-1; return ri<rj; } } void getsa() { memset(sa,0,sizeof(sa)); memset(rnk,0,sizeof(rnk)); memset(tmp,0,sizeof(tmp)); for(int i=0;i<len;i++) { sa[i]=i; rnk[i]=buf[i]; } sa[len]=len; rnk[len]=-1; for(k=1;k<=len;k*=2) { sort(sa,sa+len+1,comp); tmp[sa[0]]=0; for(int i=1;i<=len;i++) { tmp[sa[i]]=tmp[sa[i-1]]+(comp(sa[i-1],sa[i])?1:0); } for(int i=0;i<=len;i++) { rnk[i]=tmp[i]; } } } void getlcp() { getsa(); memset(rnk,0,sizeof(rnk)); memset(lcp,0,sizeof(lcp)); for(int i=0;i<=len;i++) { rnk[sa[i]]=i; } int h=0; lcp[0]=h; for(int i=0;i<len;i++) { int j=sa[rnk[i]-1]; if(h>0) h--; for(;i+h<len&&j+h<len;h++) { if(buf[i+h]!=buf[j+h]) break; } lcp[rnk[i]-1]=h; } } void debug() { for(int i=0;i<=len;i++) { int l=sa[i]; if(l==len) { printf("0\n"); } else { for(int j=sa[i];j<len;j++) { printf("%d ",buf[j]); } printf(" %d\n",lcp[i]); } } } bool judge(int l) { int cnt=0; for(int i=1;i<len;i++) { if(lcp[i]>=l)//求前缀大于等于l的连续长度 { cnt++; } else cnt=0; if(cnt==t-1) return true; } return false; } void solve() { int l=1,r=len; int ans=0; while(l<=r) { int mid=(l+r)>>1; if(judge(mid))//2分枚举长度 { ans=max(ans,mid); l=mid+1; } else r=mid-1; } printf("%d\n",ans); } int main() { while(scanf("%d%d",&len,&t)!=EOF) { for(int i=0;i<len;i++) scanf("%d",&buf[i]); getlcp(); // debug() solve(); } return 0; }
相关文章推荐
- windows系统下安装 apache+mysql+php
- Java遍历文件树的实现和测试
- 正则表达式的学习(一)
- iOS学习笔记(2) - Objective-C基础
- docker的安装--基于docker1.6
- 2015年蓝桥杯省赛B组C/C++(试题+答案)前几题
- 08 scala, imported `Record' is permanently hidden by definition of class Record in package test
- listview 嵌套listView显示不全的处理方式
- 理解C语言——从小菜到大神的晋级之路(12)——动态内存管理
- 操作系统是如何工作的
- BZOJ_P1951&Codevs_P1830 [SDOI2010]古代猪文(Lucas定理+扩展欧几里得+中国剩余定理)
- 【Usaco2015 JAN】Moovie Mooving【DP】(发现自己有点不会写二分啊)
- JavaScript第三天
- 运维如何通过学习python学会编程
- ganglia集群监控搭建步骤
- Java基本数据类型
- 完全背包 (DP)
- vmstat
- 内核同步方法
- Java并发编程:Callable、Future和FutureTask