首页 > 其他分享 >【模板】HASH

【模板】HASH

时间:2022-10-12 07:11:28浏览次数:45  
标签:begin HASH moyn char 1LL res ns 模板

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;
	}
};

额,质数可以考场现筛。

标签:begin,HASH,moyn,char,1LL,res,ns,模板
From: https://www.cnblogs.com/bikuhiku/p/hash.html

相关文章

  • 缺省源和模板
    整这个玩意单纯只是怕打比赛时要粘板子发现找不到了。缺省源先是头文件和一堆宏定义,一般都是带着的。#include<bits/stdc++.h>usingnamespacestd;typedeflonglon......
  • 18.设计模式-模板方法
    //1.定义模板抽象父类,将特有的业务定义为抽象方法,定义钩子函数//2.子类继承抽象父类,实现抽象方法//3.测试publicabstractclassCake{//定义成final,禁止子类重写......
  • P3808 【模板】AC 自动机(简单版)
    P3808KMP用来求单模式串的匹配,AC自动机用来求多模式串的匹配。就是给你n个模式串,再给你一个文本串,求有多少个模式串在文本串中出现过。AC自动机的数据结构基于trie数,像K......
  • hash算法
    hash算法是将任意长度的二进制值映射为较短的固定长度的二进制值,这个小的二进制值称为哈希值。1、哈希值是一段数据唯一且极其紧凑的数值表示形式。哈希表中元素是由哈希......
  • HashMap实现原理及源码分析
    哈希表(hashtable)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表,而HashMap的实现原理也常常出现......
  • [模板]点分治
    洛谷p3806code#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintmaxn=1e5+7;constintinf=1e7;intn,m,maxp[maxn],siz[......
  • ConcurrentHashMap底层原理
    5.6ConcurrentHashMap底层原理5.6.1jdk1.75.6.1.1数组结构数据结构是数组+segment对象,采用segment分段锁和CAS保证并发。5.6.1.2put操作流程/***Co......
  • jpa整合mybatis模板解析、hibernate整合mybatis模板解析
    jpa整合mybatis模板解析、hibernate整合mybatis模板解析jpa是hibernate的封装,主要用于spring全家桶套餐。hibernate难以编写复杂的SQL。例如一个订单查询,查询条件有时间......
  • 09-Go设计模式-模板方法模式
    模板方法模式示例代码/*模板方法模式模板方法模式中的角色和职责AbstractClass(抽象类):在抽象类中定义了一系列基本操作(PrimitiveOperations),这些基本操作可以是具体......
  • P6560 [SBCOI2020] 时光的流逝(DAG图上博弈模板)
    P6560[SBCOI2020]时光的流逝(DAG图上博弈模板)题意:​ 给出一个有向图(可能有环),每轮游戏有一个起点和终点,A和B一起玩游戏。A先移动,然后他们交替移动,当谁把棋子移动至终点,谁......