有人可以告诉我RS字符串哈希算法的工作原理或算法吗?我需要它,但在谷歌上找不到。请至少帮我解决这个算法,我会自己实现它。
答案 0 :(得分:7)
您的意思是Robert Sedgewick的字符串哈希算法吗?
uint a = 63689, uint b = 378551
foreach ( byte x ; bytes ) {
value = value * a + x;
a *= b;
}
return value;
答案 1 :(得分:0)
Python实现如下:
def RSHash(key):
a = 63689
b = 378551
hash = 0
for i in range(len(key)):
hash = hash * a + ord(key[i])
a = a * b
return hash
答案 2 :(得分:0)
C ++实现如下:
unsigned int RSHash(const std::string& str)
{
unsigned int b = 378551;
unsigned int a = 63689;
unsigned int hash = 0;
for(std::size_t i = 0; i < str.length(); i++)
{
hash = hash * a + str[i];
a = a * b;
}
return hash;
}
/* End Of RS Hash Function */