90pts 求救
查看原帖
90pts 求救
766436
Mr_RedStone楼主2024/9/29 21:17

90pts90pts WA on #5

代码如下,哈希

// #pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
int n,k;
uint64_t P1=127,P2=1e9+7;
uint64_t s1[20005],p1[20005];
uint64_t s2[20005],p2[20005];
uint64_t geth1(int l,int r){
	return s1[r]-s1[l-1]*p1[r-l+1];
}
uint64_t geth2(int l,int r){
	return s2[r]-s2[l-1]*p2[r-l+1];
}
struct Node{
	uint64_t h1,h2;
};
bool cmp(Node a,Node b){
	if(a.h1!=b.h1){
		return a.h1<b.h1;
	}
	return a.h2<b.h2;
}
bool check(int x){
	vector<Node> t;
	for(int l=1;l+x-1<=n;l++){
		int r=l+x-1;
		t.push_back({geth1(l,r),geth2(l,r)});
	}
	sort(t.begin(),t.end(),cmp);
	int cnt=0;
	for(int i=0;i<t.size();i++){
		if(i==0||t[i].h1!=t[i-1].h1||t[i].h2!=t[i-1].h2){
			cnt=0;
		}
		if(++cnt>=k){
			return 1;
		}
	}
	return 0;
}
int main(){
	scanf("%d %d",&n,&k);
	p1[0]=p2[0]=1;
	for(int i=1;i<=n;i++){
		char c;
		cin>>c;
		s1[i]=s1[i-1]*P1+c;
		p1[i]=p1[i-1]*P1;
		s2[i]=s2[i-1]*P2+c;
		p2[i]=p2[i-1]*P2;
	}
	int l=1,r=n,ans=0;
	while(l<=r){
		int m=(l+r)>>1;
		if(check(m)){
			ans=m;
			l=m+1;
		}
		else{
			r=m-1;
		}
	}
	printf("%d",ans);
	return 0;
}

我甚至用了两个哈希:(

2024/9/29 21:17
加载中...