hash(x)
作者:
RecSys
,
2021-01-25 15:32:40
,
所有人可见
,
阅读 435
/*
字符串哈希
string->num;
进制转换
hash_code
常用的一种字符串哈希函数
解决冲突
把string当作一个x进制的数字
取模运算mod
(hash_code)%2^64
uul
x取131 1331 13331
*/
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
const int X = 13331;
vector<ULL>h,x;
void BKDR_hash(string s)
{
h[0] = s[0];
x[0] = 1;
for (int i = 1; i < s.size(); i++)
{
h[i] = h[i - 1] * X + s[i];
x[i] = h[i - 1] * X;
}
}
ULL get_hash(int left, int right)
{
return left ? h[right] - h[right - left + 1] : h[right];
}
int main()
{
string s;
cin >> s;
h.resize(s.size());
x.resize(s.size());
BKDR_hash(s);
string ss;
cin >> ss;
ULL hash2 = 0;
for (int i = 0; i < ss.size(); i++)
hash2 = hash2 * X + ss[i];
for (int i = 0; i < s.size();i++)
cout << get_hash(i, min(s.size() - 1, i + ss.size() - 1)) << " ";
cout << endl;
cout << hash2;
return 0;
}