字符串哈希函数繁多,据说在信息学竞赛中,高效而易于记忆的是BKDR Hash Function
. 代码如下:
1 // BKDR Hash Function 2 int BKDRHash(char *str) 3 { 4 int seed = 131; // 31 131 1313 13131 131313 etc.. 5 int hash = 0; 6 7 while (*str) 8 { 9 hash = hash * seed + (*str++); 10 } 11 12 return (hash & 0x7FFFFFFF); 13 }