字符串hash模板

  • 字符串 Hash
  • 注意:m选择足够大的质数(至少大于字符串个数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
ll (char *s,int m){
ll h=0;
for(int i=0;s[i];i++)
h=((h<<8)+s[i])%m;
return h;
}
int main(){
#ifdef LOCAL

#endif
char a[100]="22222222222222222222222222222";
char b[100]="22222222222222222222222223222";
printf("%lldn",gethash(a,1e9+7));
printf("%lldn",gethash(b,1e9+7));
}