简陋hash算法

没怎么见过别人用过这个hash的方法吧,大概有些时候一个set就可以解决了……自己只是无聊,有一次看到了别人用类似前向星的方法来进行hash,觉得很好玩,然后自己写了一个可行的前向星结构体进行hash。恩恩,无卵用

#include
#include
const int MOD=2711827;
typedef long long LL;
struct Hash{
	LL val;
	int nxt;
}hash[MOD];
int head[MOD],tot;
bool getVis(LL n){
	int x=n%MOD;
	for(int i=head[x];~i;i=hash[i].nxt)
		if(hash[i].val==n) return true;
	return false;
}
void setVis(LL n){
	int x=n%MOD;
	hash[tot].val=n;
	hash[tot].nxt=head[x];
	head[x]=tot++;
}
int main(){
	memset(head,-1,sizeof(head));
	tot=0;
	LL n;
	while(~scanf("%lld",&n)){
		if(getVis(n)) puts("Yes");
		else puts("No");
		setVis(n);
	}
}


你可能感兴趣的:(模板)