字符串 Hash

宏定义

  • #define ull unsigned long longHash\text{Hash} 时常用 unsigned long long\text{unsigned long long} 类型。

常量和变量

  • const ull\text{const ull} P:求 Hash\text{Hash} 值时用的模数,通常为大质数。
  • const ull\text{const ull} mod:将得到的 Hash\text{Hash} 值取模在 modmod 以内,一般不用,直接对 2642^{64} 取模,自动溢出。
  • char\text{char} ji:字符集的最小字符,通常为 0aA,用于把字符化为整数。

函数

  • ull\text{ull} qmi(int a):快速幂,求 PaP^a
  • ull\text{ull} ask(string s):求 SSHash\text{Hash} 值。
  • ull\text{ull} jia(ull a,ull b,int b_size):求 Hash\text{Hash} 值为 aa 的字符串 AAHash\text{Hash} 值为 bb、长度为 b_sizeb\_size 的字符串 BB 的拼接 ABAB
  • ull\text{ull} jian(ull a,ull b,int s_size):已知 Hash\text{Hash} 值为 aa 的字符串 ABABHash\text{Hash} 值为 bb、长度为 s_sizes\_size 的字符串 AA,求 BBHash\text{Hash} 值。
#define ull unsigned long long
struct Hash{
	const ull P=13331;
	//const mod=;
	char ji='a';
	ull qmi(int a){
		ull ans=1,b=P;
		while(a){
			if(a&1)
				ans*=b;
			b=b*b;
			a>>=1;
		}
		return ans;
	}
	ull ask(string s){
		ull f=int(s[0]-ji);
		int l=s.size();
		for(int i=1;i<l;i++){
			f=f*P;
			f+=int(s[i]-ji);
		}
		return f;
	}
	ull jia(ull a,ull b,int b_size){
		return a*qmi(b_size)+b;
	}
	ull jian(ull a,ull b,int s_size){
		return a-b*qmi(s_size);
	}
};
posted @ 2022-02-25 13:27  luckydrawbox  阅读(13)  评论(0)    收藏  举报  来源