【模板】HASH

int mod_in_queue[30] = {
	998244353,993244853,1000000007,1000000021,1000000087,1000000097,1000000123,1000000207,1000000241,1000000289,1000000321,1000000363,1000000409,1000000427,1000000439,1000000453,1000000459,1000000513,1000000579,1000000607,1000000637,1000000711,1000000787,1000000801,1000000861,1000000891,1000000919,1000000933,1000000993,1000001011
};
template<unsigned int S_size>
class HASH_FUNCTION {
	public : 
	int moyn_1, moyn_2;
	unsigned int rand_seed;
	HASH_FUNCTION ()
	: moyn_1(0), moyn_2(0) {}
	void init() {
		moyn_1 = mod_in_queue[std :: chrono :: system_clock :: now().time_since_epoch().count()%30];
		moyn_2 = mod_in_queue[std :: chrono :: system_clock :: now().time_since_epoch().count()%30];
		while(moyn_1 == moyn_2) 
			moyn_2 = mod_in_queue[std :: chrono :: system_clock :: now().time_since_epoch().count()%30];
	}
	std :: pair<int,int> multi_md(char *pattern,int begin_char) {
		begin_char--;
		std :: pair<int,int> res;
		int ns_1 = 1, ns_2 = 1;
		for(int i = 0;pattern[i];++i) {
			res.first = (res.first+1LL*(pattern[i]-begin_char)*ns_1)%moyn_1;
			res.second=(res.second+1LL*(pattern[i]-begin_char)*ns_2)%moyn_2;
			ns_1 = 1LL*ns_1*(S_size+1)%moyn_1;
			ns_2 = 1LL*ns_2*(S_size+1)%moyn_2;
		}
		return res;
	}
	std :: pair<int,int> multi_md(char *begin,char *end,int begin_char) {
		begin_char--;
		std :: pair<int,int> res;
		int ns_1 = 1, ns_2 = 1;
		for(char *i = begin;i != end;++i) {
			res.first = (res.first+1LL*(*i-begin_char)*ns_1)%moyn_1;
			res.second=(res.second+1LL*(*i-begin_char)*ns_2)%moyn_2;
			ns_1 = 1LL*ns_1*(S_size+1)%moyn_1;
			ns_2 = 1LL*ns_2*(S_size+1)%moyn_2;
		}
		return res;
	}
	std :: pair<int,int> multi_md(std :: string pattern,int begin_char) {
		begin_char--;
		std :: pair<int,int> res;
		int ns_1 = 1, ns_2 = 1;
		for(auto ch : pattern) {
			res.first = (res.first+1LL*(ch-begin_char)*ns_1)%moyn_1;
			res.second=(res.second+1LL*(ch-begin_char)*ns_2)%moyn_2;
			ns_1 = 1LL*ns_1*(S_size+1)%moyn_1;
			ns_2 = 1LL*ns_2*(S_size+1)%moyn_2;
		}
		return res;
	}
};

额,质数可以考场现筛。

posted @ 2022-10-12 06:57  bikuhiku  阅读(14)  评论(0编辑  收藏  举报