【发布时间】:2011-12-05 23:41:38
【问题描述】:
这是一个前缀散列函数。我想计算这种方法中的碰撞次数,但我不知道该怎么做。看起来它可能很简单,但我想不出一个很好的方法来做到这一点......
int HashTable_qp::preHash(string & key, int tableSize )
{
string pad = "AA";
//some words in the input are less than 3 letters
//I choose to pad the string with A because all padded characters
//have same ascii val, which is low, and will hopefully alter the results less
if (key.length() < 3)
{
key.append(pad);
}
return ( key[0] + 27 * key[1] + 729 * key[2] ) % tableSize;
}
【问题讨论】:
-
创建一个无符号直方图 [tablesize],生成一些(所有)可能的字符串并计算它们的 hashval,并相应地更新直方图 histogram[hashval] +=1;
-
@wildplasser,这将是最简单的方法。我的回答会更快。如果它不是性能关键,我会选择 wild 的 想法。 (您可能应该将其发布为答案,以帮助其他人在找到此页面时找到它。)
标签: c++ hash prefix hash-collision