poj3621(hash+二分)

题目链接:http://poj.org/problem?id=3261

原本想长度从大到小扫一遍,然后TLE了,想到要找的是最大的长度,那如果大的都满足出现K次,小的自然只能出现更多次,

于是可以二分.....

 

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include<iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%I64d",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod int(1e9+7)
#define lc (d<<1)
#define rc (d<<1|1)
#define P pair<int,int>
#define pi acos(-1)
const ull base=1000007;
ull n,k,a[20008],b[20008];
ull ha[20008],dp[20008];
void as()
{
	b[0]=1;
	ha[0]=0;
	FOR(i,1,20000) b[i]=b[i-1]*base;
	FOR(i,1,n) ha[i]=ha[i-1]*base+a[i];
}
ull dog(int l,int r) {return ha[r]-ha[l-1]*b[r-l+1];}
bool jud(ull x)
{
	int kk=0;
	FOR(j,1,n-x+1) dp[kk++]=dog(j,j+x-1);
	sort(dp,dp+kk);
	ull t=dp[0],z=1;
	FOR(j,1,kk-1)
	{
		if(dp[j]==t) z++;
		else t=dp[j],z=1;
		if(z==k) return true;
	}
	return false;
}
int main()
{
    while(scanf("%llu %llu",&n,&k)!=EOF)
    {
	    FOR(i,1,n) scanf("%llu",&a[i]);
	    as();
	    ull l=1,r=n;
	    while(l+1<r)
	    {
	    	ull mid=(l+r)>>1;
	    	if(jud(mid)) l=mid;
	    	else r=mid;
	    }
	    printf("%d\n",l);
    }
    return 0;
}

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值