蒟蒻刚学 hash,请问这题如何写 hash 函数呢
查看原帖
蒟蒻刚学 hash,请问这题如何写 hash 函数呢
592238
Elairin176楼主2023/1/10 11:36
//Code by __dest__ruct__or__(uid=592238)
#include <iostream>
#include <algorithm>
using namespace std;
const int mod=10000003;
int n,t,q,s[100001],a[100001],len[100001],h[100001],m,r,ht;
long long qz[100001];
inline int _hash(int len){
	int ans=0;
	for(int i=1;i<=len;i++){
		ans=(1ll*ans+1ll*h[i]*i%mod)%mod;
	}
	return ans;
}
int main(void){
	//freopen("data.txt","r",stdin);
	scanf("%d%d%d",&n,&t,&q);
	for(int i=1;i<=t;i++){
		scanf("%d",&h[i]);
	}
	ht=_hash(t);
	for(int i=1;i<=n;i++){
		scanf("%d",&len[i]);
		for(int j=1;j<=len[i];j++){
			scanf("%d",&h[j]);
		}
		a[i]=_hash(len[i]);
	}
	scanf("%d",&m);
	for(int i=1;i<=m;i++){
		scanf("%d",&r);
		qz[i]=qz[i-1]+a[r];
		//q[i]-q[x]=ht
		int x=lower_bound(qz,qz+1+i,qz[i]-ht)-qz;
		if(qz[i]-qz[x]==ht){
			printf("%d",i);
			return 0;
		}
	}
}

这是我的代码,hash 函数有些问题。
那么问一下各位 dalao,这题的 hash 应该如何写呢?

2023/1/10 11:36
加载中...