BKDR字符串哈希
发布日期:2021-05-13 02:07:36 浏览次数:15 分类:博客文章

本文共 569 字,大约阅读时间需要 1 分钟。

BKDR字符串哈希

bkdrhash冲突的可能性非常小,但是由于\(hash\)值非常大不能映射到哈希数组地址上,所以可以通过取余,用余数作为索引地址。但这样做造成了可能的地址冲突。

#include 
#include
#include
#include
const int maxn = 10005;char s[maxn];unsigned int hash(const char *key) { char *str = const_cast
(key); unsigned int seed = 131; // 31 131 1313 13131 131313 etc.. unsigned res = 0; while (*str) { res = res * seed + (*str++); } return res;}int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", s); unsigned int res = hash(s); printf("%u\n", res); } return 0;}
上一篇:CF1465-C. Peaceful Rooks
下一篇:CF1466-D. 13th Labour of Heracles

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月21日 02时39分18秒