poj 3261 Milk Patterns (后缀数组)
来源:互联网 发布:有赞源码 编辑:程序博客网 时间:2024/05/17 02:26
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
[Submit] [Go Back] [Status] [Discuss]
题目大意:给出一个字符串,求出现至少k次的的最长重复子串,子串可重叠。
题解:后缀数组
和上一道题类似,也要用后缀数组+二分。二分答案,将height分组,然后判断每组中的个数是否>=k。
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#define N 20003using namespace std;int n,m,k,len,a[N],sa[N],rank[N],height[N],xx[N],yy[N],*x,*y,p;int b[1000003];int cmp(int i,int j,int l){return y[i]==y[j]&&(i+l>len?-1:y[i+l])==(j+l>len?-1:y[j+l]);}void get_SA(){x=xx; y=yy; m=1000000;for (int i=1;i<=n;i++) b[x[i]=a[i]]++;for (int i=1;i<=m;i++) b[i]+=b[i-1];for (int i=n;i>=1;i--) sa[b[x[i]]--]=i;for (int k=1;k<=n;k<<=1) {p=0;for (int i=n-k+1;i<=n;i++) y[++p]=i;for (int i=1;i<=n;i++) if (sa[i]>k) y[++p]=sa[i]-k;for (int i=1;i<=m;i++) b[i]=0;for (int i=1;i<=n;i++) b[x[y[i]]]++;for (int i=1;i<=m;i++) b[i]+=b[i-1];for (int i=n;i>=1;i--) sa[b[x[y[i]]]--]=y[i];swap(x,y); p=2; x[sa[1]]=1;for (int i=2;i<=n;i++) x[sa[i]]=cmp(sa[i-1],sa[i],k)?p-1:p++;if (p>len) break;m=p+1; }p=0;for (int i=1;i<=n;i++) rank[sa[i]]=i;for (int i=1;i<=n;i++) {if (rank[i]==1) continue;int j=sa[rank[i]-1];while (i+p<=n&&j+p<=n&&a[i+p]==a[j+p]) p++;height[rank[i]]=p;p=max(0,p-1);}}int pd(int x){int size=1;for (int i=2;i<=n;i++) if (height[i]>=x) size++; else { if (size>=k) return 1; size=1; } if (size>=k) return 1;return 0;}int main(){freopen("a.in","r",stdin);freopen("my.out","w",stdout);scanf("%d%d",&n,&k); len=n;for (int i=1;i<=n;i++) scanf("%d",&a[i]);get_SA();int l=0; int r=n; int ans=0;while (l<=r) {int mid=(l+r)/2;if (pd(mid)) ans=max(ans,mid),l=mid+1;else r=mid-1;}printf("%d\n",ans);}
- Milk Patterns(poj 3261,后缀数组)
- poj 3261 Milk Patterns(后缀数组)
- POJ 3261 Milk Patterns (后缀数组)
- poj 3261 Milk Patterns(后缀数组)
- poj 3261 Milk Patterns (后缀数组)
- POJ 3261Milk 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 (后缀数组)
- POJ 3261 Milk Patterns (后缀数组)
- POJ 3261 Milk Patterns 后缀数组+二分
- poj 3261 Milk Patterns (后缀数组)
- 5-3 重要的话说三遍 (5分)
- IE 下bootstraptable表单搜索汉字失败
- 装Yii遇到的小问题
- swiper display:none 后 在显示 滑动问题
- Java中的Servelt讲解
- poj 3261 Milk Patterns (后缀数组)
- Golang开发环境搭建-Vim篇
- 关于Object类的几个方法
- Android Developer - App Widgets
- 退出框
- java面试题(三)
- HiveServer2表结构变更太耗时分析
- 【数据库学习】 之 函数依赖及其公理/定理
- 5-4 后天 (5分)