POJ 3261 Milk Patterns 后缀数组+二分
来源:互联网 发布:pop3 smtp 端口 编辑:程序博客网 时间:2024/05/03 20:00
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
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 212323231
Sample Output
4
Source
求在一个序列当中重复出现至少k次的最长子序列的长度。
把数字当做字符串,建立后缀数组。
之后二分答案len。同样地把后缀按照长度len分为若干组,若某组的字符串个数大于等于k则存在满足条件的长度为len的字符串。
入门级别的题目。
#include <cstdio>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=20005,inf=0x3f3f3f3f; const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L);int wa[maxn],wb[maxn],wv[maxn],ws[maxn],sa[maxn],ranki[maxn],height[maxn];int s[maxn];int cmp(int *r,int a,int b,int l) {return r[a]==r[b]&&r[a+l]==r[b+l];}void build(int *r,int *sa,int n,int m) {int i,j,k,p,*x=wa,*y=wb,*t;for (i=0;i<m;i++) ws[i]=0;for (i=0;i<n;i++) ws[x[i]=r[i]]++;for (i=0;i<m;i++) ws[i]+=ws[i-1];for (i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;for (j=1,p=1;p<n;j*=2,m=p) {for (p=0,i=n-j;i<n;i++) y[p++]=i;for (i=0;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j;for (i=0;i<n;i++) wv[i]=x[y[i]];for (i=0;i<m;i++) ws[i]=0;for (i=0;i<n;i++) ws[wv[i]]++;for (i=1;i<m;i++) ws[i]+=ws[i-1];for (i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];t=x;x=y;y=t;p=1;x[sa[0]]=0;for (i=1;i<n;i++) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;}for (i=1;i<n;i++) ranki[sa[i]]=i;k=0; for (i=0;i<n-1;height[ranki[i++]]=k) {if (k) k--;for (j=sa[ranki[i]-1];r[i+k]==r[j+k];k++);}}bool check(int l,int n,int k) {int i,mi,ma,cnt=1;for (i=2;i<=n;i++) {if (height[i]>=l) cnt++; else {if (cnt>=k) return true;cnt=1;}}if (cnt>=k) return true;return false;}int main() {int n,k;while (scanf("%d%d",&n,&k)!=EOF) {int i,m=-1;for (i=0;i<n;i++) {scanf("%d",&s[i]);m=max(m,s[i]); }s[n]=0;build(s,sa,n+1,m+1);int l=1,r=n,mid,ans=-1; while (l<=r) { mid=(l+r)/2; if (check(mid,n,k)) ans=mid,l=mid+1; else r=mid-1; } printf("%d\n",ans); }return 0;}
- POJ 3261 Milk Patterns 后缀数组+二分
- poj 3261 Milk Patterns 后缀数组 二分
- 【poj 3261】Milk Patterns 二分+后缀数组
- poj 3261 Milk Patterns 后缀数组+二分
- POJ-3261 Milk Patterns,后缀数组+二分。。
- POJ 3261 Milk Patterns <后缀数组+二分>
- poj 3261 Milk Patterns(后缀数组,二分)
- |poj 3261|后缀数组|二分|Milk Patterns
- poj 3261 Milk Patterns (后缀数组+二分)
- POJ 3261 Milk Patterns 后缀数组+二分
- Milk Patterns+POJ+后缀数组+二分
- [后缀数组+离散化+二分] poj 3261 Milk Patterns
- poj 3261 Milk Patterns (后缀数组+二分答案)
- POJ 3261 Milk Patterns(二分+后缀数组)
- POJ 3261 Milk Patterns(后缀数组+二分)
- POJ 3261 Milk Patterns 后缀数组 二分答案
- [poj 3261] Milk Patterns:二分,哈希或后缀数组
- Poj 3261 Milk Patterns(后缀数组+二分答案)
- 自定义Adapter的getView方法的含义
- 难以分类编程题(不断更新)
- 腾讯2018校招WEB前端开发笔试有感
- javastript实现栈
- 智慧教室与未来教育
- POJ 3261 Milk Patterns 后缀数组+二分
- 设计模式六大原则
- PHP redis库
- 可被路由协议、路由协议、不可路由协议的区别
- ccf认证_分蛋糕 编号201703-1
- 文件夹操作命令笔记
- 使用OPENCV简单实现具有肤质保留功能的磨皮增白算法
- Java中getResourceAsStream的用法
- HDU OJ 1005 Number Sequence