POJ-3261 后缀数组 对height分组 二分

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

二分答案,然后将后缀分成若干组。每组里面的height>=mid
判断的是有否有一个组的后缀个数>=k
如果有,那么存在k个相同的子串满足条件,否则不存在。

#include<iostream>
#include<stdio.h>
#include<string.h>
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
const int maxn=1e6+3;
const int N=2e4+3;
int wa[N],wb[N],wv[N],c[maxn];
int c0(int *r,int a,int b){
	return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];
}
int c12(int k,int *r,int a,int b){
	if(k==2)return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1);
	return r[a]<r[b]||r[a]==r[b]&&wv[a+1]<wv[b+1];
}
void sort(int *r,int *a,int *b,int n,int m){
	int i;
	for(i=0;i<n;++i) wv[i]=r[a[i]];
	for(i=0;i<m;++i) c[i]=0;
	for(i=0;i<n;++i) ++c[wv[i]];
	for(i=1;i<m;++i) c[i]+=c[i-1];
	for(i=n-1;i>=0;--i) b[--c[wv[i]]]=a[i];
}
void dc3(int *r,int *sa,int n,int m){
	int i,j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;
	r[n]=r[n+1]=0;
	for(i=0;i<n;++i)if(i%3!=0)wa[tbc++]=i;
	sort(r+2,wa,wb,tbc,m);
	sort(r+1,wb,wa,tbc,m);
	sort(r,wa,wb,tbc,m);
	for(p=1,rn[F(wb[0])]=0,i=1;i<tbc;++i)rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++;
	if(p<tbc) dc3(rn,san,tbc,p);
	else for(i=0;i<tbc;++i) san[rn[i]]=i;
	for(i=0;i<tbc;++i) if(san[i]<tb) wb[ta++]=san[i]*3;
	if(n%3==1) wb[ta++]=n-1;
	sort(r,wb,wa,ta,m);
	for(i=0;i<tbc;i++) wv[wb[i]=G(san[i])]=i;
	for(i=0,j=0,p=0;i<ta&&j<tbc;++p)sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++];
	for(;i<ta;++p) 	sa[p]=wa[i++];
	for(;j<tbc;++p) sa[p]=wb[j++];
}
int r[N*3],sa[N*3],Rank[N],height[N],L,R;//r,sa数组要开3倍
int a[N],n,k,ma;
void calheight(int *r,int *sa,int n){//计算height
	int i,j,k=0;
	for(i=0;i<=n;++i) Rank[sa[i]]=i;
	for(i=0;i<n;height[Rank[i++]]=k,R=max(R,k))
		for(k?--k:0,j=sa[Rank[i]-1];r[i+k]==r[j+k];++k);
}
bool C(int x){
	int cnt=1;
	for(int i=2;i<=n;++i){
		if(height[i]<x)cnt=1;
		else ++cnt;
		if(cnt>=k)return 1;
	}
	return 0;
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;++i)scanf("%d",&r[i]),++r[i],ma=max(ma,r[i]);
	dc3(r,sa,n+1,ma+1);
	calheight(r,sa,n);
	while(L<R){
		int mid=(L+R+1)>>1;
		if(C(mid))L=mid;
		else R=mid-1;
	}
	printf("%d\n",L);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值