一个简单的hash
#include <iostream>
#include <cstdio>
using namespace std;
unsigned ans=0;
unsigned BKDRHash(char *str)
{
unsigned seed = 131; // 31 131 1313 13131 131313 etc..
unsigned hash = 0;
while (*str)hash = hash * seed + (*str++);
return (hash & 0x7FFFFFFF);
}
int main()
{
scanf("%s",a);
ans=BKDRHash(a);
printf("%u\n",ans);
return 0;
}

本文介绍了一种简单的字符串哈希算法BKDRHash,并提供了完整的C++实现代码。该算法通过循环遍历输入字符串中的每个字符并使用特定的种子值进行哈希计算,最终返回一个整数作为哈希值。
3万+

被折叠的 条评论
为什么被折叠?



