原文 " B: G2 U3 i, w' ] g7 u4 h, n* O
http://blog.csai.cn/user3/50125/archives/2009/35638.html - w' \+ h- e& D& L/ J7 M( y! X
/ H% h% \9 P+ Q
[pre]文章转摘自http://www.cmykrgb123.cn/blog/string-hash-compare/ }4 ~! O0 ?' g, [* W
# a! j4 N1 [ p+ b1 E' P9 k
常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法。这些函数使用 / g; C7 ^2 x7 O" E$ Z. @/ N
位运算使得每一个字符都对最后的函数值产生影响。另外还有以MD5和SHA1为代表的杂凑函数,
5 o+ |' o( F2 H这些函数几乎不可能找到碰撞。 $ ^+ @9 q- e! `. _* C! f0 U, J+ y
常用字符串哈希函数有BKDRHash,APHash,DJBHash,JSHash,RSHash,SDBMHash, & O* c2 Z" s* a
PJWHash,ELFHash等等。对于以上几种哈希函数,我对其进行了一个小小的评测。 3 E) B2 H7 p3 k' {1 ~
Hash函数 | 数据1 | 数据2 | 数据3 | 数据4 | 数据1得分 | 数据2得分 | 数据3得分 | 数据4得分 | 平均分 | BKDRHash | 2 | 0 | 4774 | 481 | 96.55 | 100 | 90.95 | 82.05 | 92.64 | APHash | 2 | 3 | 4754 | 493 | 96.55 | 88.46 | 100 | 51.28 | 86.28 | DJBHash | 2 | 2 | 4975 | 474 | 96.55 | 92.31 | 0 | 100 | 83.43 | JSHash | 1 | 4 | 4761 | 506 | 100 | 84.62 | 96.83 | 17.95 | 81.94 | RSHash | 1 | 0 | 4861 | 505 | 100 | 100 | 51.58 | 20.51 | 75.96 | SDBMHash | 3 | 2 | 4849 | 504 | 93.1 | 92.31 | 57.01 | 23.08 | 72.41 | PJWHash | 30 | 26 | 4878 | 513 | 0 | 0 | 43.89 | 0 | 21.95 | ELFHash | 30 | 26 | 4878 | 513 | 0 | 0 | 43.89 | 0 | 21.95 | : p5 C9 y( R. v% L
其中数据1为100000个字母和数字组成的随机串哈希冲突个数。数据2为100000个有意义的英文句 b/ a* G1 A; p9 v
子哈希冲突个数。数据3为数据1的哈希值与1000003(大素数)求模后存储到线性表中冲突的个数。 8 M7 u# d0 L: K
数据4为数据1的哈希值与10000019(更大素数)求模后存储到线性表中冲突的个数。
+ E4 [" q+ @* e4 ]( _ J1 Q经过比较,得出以上平均得分。平均数为平方平均数。可以发现,BKDRHash无论是在实际效果还是
( N: k7 d6 h; j' x编码实现中,效果都是最突出的。APHash也是较为优秀的算法。DJBHash,JSHash,RSHash与
) w2 Q/ g, u% w/ q( G- W5 ]; j! H5 m# hSDBMHash各有千秋。PJWHash与ELFHash效果最差,但得分相似,其算法本质是相似的。
" Y$ `: O6 n# \在信息修竞赛中,要本着易于编码调试的原则,个人认为BKDRHash是最适合记忆和使用的。 - f l! c7 n2 g
CmYkRgB123原创,欢迎建议、交流、批评和指正。
+ H3 E( g7 x6 K 7 l: i* o6 v5 x
附:各种哈希函数的C语言程序代码
; \8 l* E, W9 _5 n4 g, ]
, E/ m% M3 ]' T+ T$ I* q' {) cunsigned int SDBMHash(char *str)
$ [) A- p( l$ P' F1 a' P$ Z{
" Q3 J2 k" b4 N& P- { unsigned int hash = 0; " h8 ^) \! `# R# |( C( X
7 I5 D$ M: \! N: y. Y' J3 [3 b! [
while (*str) ) [1 h3 l8 S3 G' i
{ D% i! d! l9 U% t/ q2 _7 Y
// equivalent to: hash = 65599*hash + (*str++); 0 c7 o5 S9 ]$ a) Z: c: r5 h; B l3 X; R
hash = (*str++) + (hash << 6) + (hash << 16) - hash; , ]$ P0 A: Z N# m% W* f
} 5 v, S) ]2 Q8 i3 G6 T$ V
. H9 X8 G v4 A9 N7 J
return (hash & 0x7FFFFFFF);
0 `$ m! f0 l" M; ~1 h} ' X3 u. x" |/ `7 ]/ @+ }' i! K
& _7 b; y: l- U0 J' L// RS Hash 3 x7 w7 ~+ c6 A6 E; e. x. ^% Z
unsigned int RSHash(char *str) & r1 Z( @3 `- F! [, y
{
R& w9 b2 ? {: x" T' |2 k/ q, C8 d unsigned int b = 378551; B0 e/ g% h7 ^3 z1 Y6 w' a) }
unsigned int a = 63689;
1 Y( [8 J6 Z v7 s% g7 C, T3 _; s unsigned int hash = 0;
( p' M; l( c& D% ~/ ?
1 H" U) u% }$ B1 H while (*str)
" E- O: v. ^; G3 T {
2 U% `9 c5 P N* h hash = hash * a + (*str++); 5 t9 X+ F! _! }% y6 q
a *= b; 3 @2 D e; k' I2 m1 f
}
' f* ?& h, Z, |$ |( Q3 A $ U' P6 x, Q, o- D2 t& S
return (hash & 0x7FFFFFFF); 6 E8 N' s* u* f9 w8 D6 u0 b
} 0 C" D6 E, L: ]6 b
1 p) ]% r8 n( m, w5 Z" z% I+ d// JS Hash
% Y4 A( r! c2 y" l1 `# g/ Tunsigned int JSHash(char *str)
; }0 ]$ L1 U4 P/ R y* ^( f{
6 b3 `8 u2 {( w/ X unsigned int hash = 1315423911; % R( F+ M2 i! G7 d
E+ y9 T2 r( F' R* u7 } while (*str) % E" f J7 c1 m: B% _
{
! h1 `* G( q9 u2 j0 |, \ hash ^= ((hash << 5) + (*str++) + (hash >> 2));
) ^+ \) Z, _# @9 e2 T% u, K0 ~ } # E1 q+ }. B. p7 x
8 e0 y; @! q/ Z, r' K* O
return (hash & 0x7FFFFFFF); 9 Z v0 q' r2 w6 ]3 J& H2 k
}
0 I! C: T2 f3 n% H+ b1 v . b; B( G: u4 q3 ]7 e
// P. J. Weinberger Hash 1 E) h- X& g% T- R; n3 k
unsigned int PJWHash(char *str): {- _3 o- z* x5 [9 c
{
- c$ e* _8 z. ?/ p2 c unsigned int BitsInUnignedInt = (unsigned int)(sizeof(unsigned int) * 8);
! t8 [# J# X! L: n unsigned int ThreeQuarters = (unsigned int)((BitsInUnignedInt * 3) / 4);
9 j' `7 a+ Y4 a, C3 [ unsigned int OneEighth = (unsigned int)(BitsInUnignedInt / 8);6 i9 Q$ \" a0 j# R5 L
unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnignedInt [pre] - OneEighth); unsigned int hash = 0; unsigned int test = 0; while (*str) { hash = (hash << OneEighth) + (*str++); if ((test = hash & HighBits) != 0) { hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits)); } } return (hash & 0x7FFFFFFF);} // ELF Hash unsigned int ELFHash(char *str){ unsigned int hash = 0; unsigned int x = 0; while (*str) { hash = (hash << 4) + (*str++); if ((x = hash & 0xF0000000L) != 0) { hash ^= (x >> 24); hash &= ~x; } } return (hash & 0x7FFFFFFF);} // BKDR Hash unsigned int BKDRHash(char *str){ unsigned int seed = 131; // 31 131 1313 13131 131313 etc.. unsigned int hash = 0; while (*str) { hash = hash * seed + (*str++); } return (hash & 0x7FFFFFFF);} // DJB Hash unsigned int DJBHash(char *str){ unsigned int hash = 5381; while (*str) { hash += (hash << 5) + (*str++); } return (hash & 0x7FFFFFFF);} // AP Hash unsigned int APHash(char *str){ unsigned int hash = 0; int i; for (i=0; *str; i++) { if ((i & 1) == 0) { hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3)); } else { hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5))); } } return (hash & 0x7FFFFFFF);}[/pre] |