如何为36个字符串编写一个完美的哈希函数?
使用数字 0-5,我需要为包含 6 个字符和 3 个整数的 36 个学生 ID 字符串编写一个完美的 has 函数(例如 BYPLOK120)。
哈希函数如下所示:
String [] studentID = ...
是 36 个唯一字符串,格式为 (xxx/yyy/zzz),其中 x 和 y 分别是学生姓氏和名字的三个字母。 z 是 3 个随机数。
int hashTableSize = 37;`
int [] hashValue = new int[9];`
int [] weights = {1,0,5,4,3,2,5,1,2};
权重是 0-5 之间的 9 个数字。这是我需要弄清楚的部分 - 大约有 200 万种组合 (5^9),不到 10 种组合就能为您提供完美的哈希函数。
for (int i = 0; i < hashTableSize; i++)
for (int j = 0; j < studentID[I].length; j++)
hashVal[i] += ( studentID[i].charAt(j) * weight[j] ) % hashTableSize;
我需要数组中每个字符串的哈希值是唯一的(即插入时不会发生冲突)
我有重复的哈希值,因为我找不到唯一的权重整数数组。
Using the numbers 0-5 I need to write a perfect has function for 36 Student ID Strings with six characters and 3 integers (e.g. BYPLOK120).
The hash function looks something like as follows:
String [] studentID = ...
is 36 unique Strings in the format (xxx/yyy/zzz) where x and y are three letters from the students last and first names, respectively. z are 3 random numbers.
int hashTableSize = 37;`
int [] hashValue = new int[9];`
int [] weights = {1,0,5,4,3,2,5,1,2};
Weights is 9 numbers between 0-5. THIS IS THE PART I NEED TO FIGURE OUT - there are about 2 million combinations (5^9) and less than 10 will give you a perfect hash function.
for (int i = 0; i < hashTableSize; i++)
for (int j = 0; j < studentID[I].length; j++)
hashVal[i] += ( studentID[i].charAt(j) * weight[j] ) % hashTableSize;
I need the hash value of each string in the array to be unique (i.e. there are no collisions when inserting)
I had duplicate hash values as I could not find the unique array of weight integers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论