【POJ3261】Milk Patterns 后缀数组

水题不好意思说题解。

说说题意吧:

给一个字符串(数字串),然后求最长k次重复子串。

即某串在字符串中重复了至少k次,求这种串的最长长度。

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 21000
using namespace std;
struct LSH
{
	int x,id;
	bool operator < (const LSH &a)const{return x<a.x;}
}lsh[N];
int s[N];
int sa[N],rank[N],h[N],n,m,len;
int cnt[N],val[N],stk[N],_val[N],top;
bool issame(int a,int b,int hl)
{
	return val[a]==val[b]&&
	((a+hl>=len&&b+hl>=len)||(a+hl<len&&b+hl<len&&val[a+hl]==val[b+hl]));
}
void SA(int lim)
{
	int i,j,k,hl;
	for(i=0;i<lim;i++)cnt[i]=0;
	for(i=0;i<len;i++)cnt[val[i]=s[i]]++;
	for(i=1;i<lim;i++)cnt[i]+=cnt[i-1];
	for(i=len-1;i>=0;i--)sa[--cnt[val[i]]]=i;

	for(k=1;;k++)
	{
		top=0,hl=1<<(k-1);
		for(i=0;i<len;i++)if(sa[i]+hl>=len)stk[top++]=sa[i];
		for(i=0;i<len;i++)if(sa[i]>=hl)stk[top++]=sa[i]-hl;

		for(i=0;i<lim;i++)cnt[i]=0;
		for(i=0;i<len;i++)cnt[val[i]]++;
		for(i=1;i<lim;i++)cnt[i]+=cnt[i-1];
		for(i=len-1;i>=0;i--)sa[--cnt[val[stk[i]]]]=stk[i];

		for(lim=i=0;i<len;lim++)
		{
			for(j=i;j<len-1&&issame(sa[j],sa[j+1],hl);j++);
			for(;i<=j;i++)_val[sa[i]]=lim;
		}
		for(i=0;i<len;i++)val[i]=_val[i];
		if(lim==len)break;
	}

	for(i=0;i<len;i++)rank[sa[i]]=i;
	for(k=i=0;i<len;i++)
	{
		if(k)k--;
		if(!rank[i])continue;
		while(s[i+k]==s[sa[rank[i]-1]+k])k++;
		h[rank[i]]=k;
	}
}
int K;
bool check(int mid)
{
	int num=1,i;
	for(i=0;i<len;i++)
	{
		if(h[i]>=mid)num++;
		else num=1;
		if(num>=K)return 1;
	}
	return 0;
}
int main()
{
//	freopen("test.in","r",stdin);
	int i,j,k;
	while(scanf("%d%d",&len,&K)!=EOF)
	{
		for(i=0;i<len;i++)scanf("%d",&lsh[i].x),lsh[i].id=i;
		sort(lsh,lsh+len);
		int nm=0;
		for(i=0;i<len;i++)
		{
			if(i==0||lsh[i].x!=lsh[i-1].x)nm++;
			s[lsh[i].id]=nm;
		}
		SA(20000);
		int l=0,r=len,mid,ans=0;
		while(l<=r)
		{
			if(r-l<=3)
			{
				for(i=l;i<=r;i++)if(check(i))ans=i;
					break;
			}
			mid=l+r>>1;
			if(check(mid))l=mid;
			else r=mid;
		}
		printf("%d\n",ans);
	}
	return 0;
}
时间: 2024-10-27 12:27:33

【POJ3261】Milk Patterns 后缀数组的相关文章

[POJ3261] Milk Patterns (后缀数组+二分)

题目概述: 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 t

poj 3261 Milk Patterns 后缀数组+二分

1 /*********************************************************** 2 题目: Milk Patterns(poj 3261) 3 链接: http://poj.org/problem?id=3261 4 题意: 给一串数字,求这些数字中公共子串个数大于k的 5 最长串. 6 算法: 后缀数组+二分 7 ***********************************************************/ 8 #incl

POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次

Milk Patterns 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 r

POJ 3261 Milk Patterns 后缀数组

用后缀数组求重复出现至少k次的可重叠最长子串的长度, 当然是可以用hash搞的,用后缀数组的话,只要在分组之后看看个数是不是大于等于k #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <stack> #include <map> #include <set> #include <climits>

POJ 3261 Milk Patterns ( 后缀数组 &amp;&amp; 出现k次最长可重叠子串长度 )

题意 : 给出一个长度为 N 的序列,再给出一个 K 要求求出出现了至少 K 次的最长可重叠子串的长度 分析 : 后缀数组套路题,思路是二分长度再对于每一个长度进行判断,判断过程就是对于 Height 数组进行限定长度的分组策略,如果有哪一组的个数 ≥  k 则说明可行! 分组要考虑到一个事实,对于每一个后缀,与其相匹配能够产生最长的LCP长度的串肯定是在后缀数组中排名与其相邻. 一开始对分组的理解有误,所以想了一个错误做法 ==> 遍历一下 Height 将值 ≥ (当前二分长度) 的做一次贡

[Poj3261] [Bzoj1717] [后缀数组论文例题,USACO 2006 December Gold] Milk Patterns [后缀数组可重叠的k次最长重复子串]

和上一题(POJ1743,上一篇博客)相似,只是二分的判断条件是:是否存在一段后缀的个数不小于k 1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cmath> 7 #include <ctime> 8 #include <map&

POJ - 3261 Milk Patterns (后缀数组求可重叠的 k 次最长重复子串)

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 pattern

poj3261 Milk Patterns(后缀数组)

Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 12571 Accepted: 5572 Case Time Limit: 2000MS Description Farmer John has noticed that the quality of milk given by his cows varies from day to day. On further investigation, he

poj3261Milk Patterns 后缀数组

题目地址:http://poj.org/problem?id=3261 题目: 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