哈希

哈希素数

const ll bas[]={655717,1635947,2756749,5496013,15485863,21742157,31871237,40114259};

线性探测

namespace Hash {
	const int hashmod = 1635947;
	int v[hashmod];
	ll h[hashmod];
	bool vis[hashmod];
	void init() {
		memset(v, 0, sizeof v);
		memset(vis, 0, sizeof vis);
	}
	int &get(unsigned long long S) {
		int t2 = S%hashmod, i;
		for (i = t2; vis[i]; i = (i + 1) % hashmod);
		h[i] = S, vis[i] = 1;
		return v[i];
	}
};

 链表

namespace Hash {
	const int hashmod = 1635947;
	int next[hashmod], v[hashmod], head[hashmod], cnt;
	ll h[hashmod];
	void init() {
		memset(head,-1,sizeof head);
		memset(v,0,sizeof v);
		cnt = 0;
	}
	int &get(unsigned long long x) {
		int y = x%hashmod;
		for(int i=head[y]; i!=-1; i=next[i]) if(h[i]==x) return v[i];
		h[cnt]=x,next[cnt]=head[y],head[y]=cnt++;
		return v[cnt-1];
	}
}

 

unordered_map 重写hash函数

#include <unordered_map>
struct Hash {
    long long operator() (long long x) const {
        x+=0x9e3779b97f4a7c15;
        x=(x^(x>>30))*0xbf58476d1ce4e5b9;
        x=(x^(x>>27))*0x94d049bb133111eb;
        return x^(x>>31);
    }
};

unordered_map<long long,int,Hash> s;

 

转载于:https://www.cnblogs.com/uid001/p/10870928.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值