转自:http://blog.csai.cn/user3/50125/archives/2009/35638.html
http://www.cnblogs.com/xinzaibing/archive/2010/03/15/1686452.html
常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法。这些函数使用
位运算使得每一个字符都对最后的函数值产生影响。另外还有以MD5和SHA1为代表的杂凑函数,
这些函数几乎不可能找到碰撞。
常用字符串哈希函数有BKDRHash,APHash,DJBHash,JSHash,RSHash,SDBMHash,
PJWHash,ELFHash等等。对于以上几种哈希函数,我对其进行了一个小小的评测。
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
其中数据1为100000个字母和数字组成的随机串哈希冲突个数。数据2为100000个有意义的英文句
子哈希冲突个数。数据3为数据1的哈希值与1000003(大素数)求模后存储到线性表中冲突的个数。
数据4为数据1的哈希值与10000019(更大素数)求模后存储到线性表中冲突的个数。
经过比较,得出以上平均得分。平均数为平方平均数。可以发现,BKDRHash无论是在实际效果还是
编码实现中,效果都是最突出的。APHash也是较为优秀的算法。DJBHash,JSHash,RSHash与
SDBMHash各有千秋。PJWHash与ELFHash效果最差,但得分相似,其算法本质是相似的。
在信息修竞赛中,要本着易于编码调试的原则,个人认为BKDRHash是最适合记忆和使用的。
CmYkRgB123原创,欢迎建议、交流、批评和指正。
附:各种哈希函数的C语言程序代码
unsigned int SDBMHash(char *str)
{
unsigned int hash = 0;
while (*str)
{
// equivalent to: hash = 65599*hash + (*str++);
hash = (*str++) + (hash << 6) + (hash << 16) - hash;
}
return (hash & 0x7FFFFFFF);
}
// RS Hash
unsigned int RSHash(char *str)
{
unsigned int b = 378551;
unsigned int a = 63689;
unsigned int hash = 0;
while (*str)
{
hash = hash * a + (*str++);
a *= b;
}
return (hash & 0x7FFFFFFF);
}
// JS Hash
unsigned int JSHash(char *str)
{
unsigned int hash = 1315423911;
while (*str)
{
hash ^= ((hash << 5) + (*str++) + (hash >> 2));
}
return (hash & 0x7FFFFFFF);
}
// P. J. Weinberger Hash
unsigned int PJWHash(char *str)
{
unsigned int BitsInUnignedInt = (unsigned int)(sizeof(unsigned int) * 8);
unsigned int ThreeQuarters = (unsigned int)((BitsInUnignedInt * 3) / 4);
unsigned int OneEighth = (unsigned int)(BitsInUnignedInt / 8);
unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnignedInt
- 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);
}
暴雪公司有个经典的字符串的hash公式
先提一个简单的问题,假如有一个庞大的字符串数组,然后给你一个单独的字符串,让你从这个数组中查找是否有这个字符串并找到它,你会怎么做? 有一个方法最简单,老老实实从头查到尾,一个一个比较,直到找到为止,我想只要学过程序设计的人都能把这样一个程序作出来,但要是有程序员把这样的程序交给用户,我只能用无语来评价,或许它真的能工作,但...也只能如此了。 最合适的算法自然是使用HashTable(哈希表),先介绍介绍其中的基本知识,所谓Hash,一般是一个整数,通过某种算法,可以把一个字符串"压缩" 成一个整数,这个数称为Hash,当然,无论如何,一个32位整数是无法对应回一个字符串的,但在程序中,两个字符串计算出的Hash值相等的可能非常小,下面看看在MPQ中的Hash算法

{
unsigned char * key = (unsigned char * )lpszFileName;
unsigned long seed1 = 0x7FED7FED , seed2 = 0xEEEEEEEE ;
int ch;
while ( * key != 0 )
{
ch = toupper( * key );
seed1 = cryptTable[(dwHashType < < 8 ) ch] ^ (seed1 seed2);
seed2 = ch seed1 seed2 (seed2 < < 5 ) 3 ;
}
return seed1;
}
Blizzard的这个算法是非常高效的,被称为"One-Way Hash",举个例子,字符串"unitneutralacritter.grp"通过这个算法得到的结果是0xA26067F3。 是不是把第一个算法改进一下,改成逐个比较字符串的Hash值就可以了呢,答案是,远远不够,要想得到最快的算法,就不能进行逐个的比较,通常是构造一个哈希表(Hash Table)来解决问题,哈希表是一个大数组,这个数组的容量根据程序的要求来定义,例如1024,每一个Hash值通过取模运算 (mod)对应到数组中的一个位置,这样,只要比较这个字符串的哈希值对的位置又没有被占用,就可以得到最后的结果了,想想这是什么速度?是的,是最快的O(1),现在仔细看看这个算法吧

{
int nHash = HashString(lpszString), nHashPos = nHash % nTableSize;
if (lpTable[nHashPos].bExists && ! strcmp(lpTable[nHashPos].pString, lpszString))
return nHashPos;
else
return - 1 ; // Error value
}
看到此,我想大家都在想一个很严重的问题:"假如两个字符串在哈希表中对应的位置相同怎么办?",究竟一个数组容量是有限的,这种可能性很大。解决该问题的方法很多,我首先想到的就是用"链表",感谢大学里学的数据结构教会了这个百试百灵的法宝,我碰到的很多算法都可以转化成链表来解决,只要在哈希表的每个入口挂一个链表,保存所有对应的字符串就OK了。 事情到此似乎有了完美的结局,假如是把问题独自交给我解决,此时我可能就要开始定义数据结构然后写代码了。然而Blizzard的程序员使用的方法则是更精妙的方法。基本原理就是:他们在哈希表中不是用一个哈希值而是用三个哈希值来校验字符串。 中国有句古话"再一再二不能再三再四",看来Blizzard也深得此话的精髓,假如说两个不同的字符串经过一个哈希算法得到的入口点一致有可能,但用三个不同的哈希算法算出的入口点都一致,那几乎可以肯定是不可能的事了,这个几率是1:18889465931478580854784,大概是10的 22.3次方分之一,对一个游戏程序来说足够安全了。 现在再回到数据结构上,Blizzard使用的哈希表没有使用链表,而采用"顺延"的方式来解决问题,看看这个算法:

{
const int HASH_OFFSET = 0 , HASH_A = 1 , HASH_B = 2 ;
int nHash = HashString(lpszString, HASH_OFFSET);
int nHashA = HashString(lpszString, HASH_A);
int nHashB = HashString(lpszString, HASH_B);
int nHashStart = nHash % nTableSize, nHashPos = nHashStart;
while (lpTable[nHashPos].bExists)
{
if (lpTable[nHashPos].nHashA == nHashA && lpTable[nHashPos].nHashB == nHashB)
return nHashPos;
else
nHashPos = (nHashPos 1 ) % nTableSize;
if (nHashPos == nHashStart)
break ;
}
return - 1 ; // Error value
}
1. 计算出字符串的三个哈希值(一个用来确定位置,另外两个用来校验)
2. 察看哈希表中的这个位置
3. 哈希表中这个位置为空吗?假如为空,则肯定该字符串不存在,返回
4. 假如存在,则检查其他两个哈希值是否也匹配,假如匹配,则表示找到了该字符串,返回
5. 移到下一个位置,假如已经越界,则表示没有找到,返回
6. 看看是不是又回到了原来的位置,假如是,则返回没找到 7. 回到3
以下是简单封装
/// //
// Name: HashAlgo.h
// Purpose: 使用魔兽Hash算法,实现索引表的填充和查找功能。
// Author: 陈相礼
// Modified by:
// Created: 07/30/09
// RCS-ID: $Id: treetest.h 43021 2009-07-30 16:36:51Z VZ $
// Copyright: (C) Copyright 2009, TSong Corporation, All Rights Reserved.
// Licence:
/// //
#define MAXFILENAME 255 // 最大文件名长度
#define MAXTABLELEN 1024 // 默认哈希索引表大小
//
// 测试宏定义,正式使用时关闭
#define DEBUGTEST 1
//
// 哈希索引表定义
typedef struct
{
long nHashA;
long nHashB;
bool bExists;
char test_filename[MAXFILENAME];
// ......
} MPQHASHTABLE;
//
// 对哈希索引表的算法进行封装
class CHashAlgo
{
public :
#if DEBUGTEST
long testid; // 测试之用
#endif
CHashAlgo( const long nTableLength = MAXTABLELEN ) // 创建指定大小的哈希索引表,不带参数的构造函数创建默认大小的哈希索引表
{
prepareCryptTable();
m_tablelength = nTableLength;
m_HashIndexTable = new MPQHASHTABLE[nTableLength];
for ( int i = 0 ; i < nTableLength; i ++ )
{
m_HashIndexTable[i].nHashA = - 1 ;
m_HashIndexTable[i].nHashB = - 1 ;
m_HashIndexTable[i].bExists = false ;
m_HashIndexTable[i].test_filename[ 0 ] = ' \0 ' ;
}
}
void prepareCryptTable(); // 对哈希索引表预处理
unsigned long HashString( char * lpszFileName, unsigned long dwHashType); // 求取哈希值
long GetHashTablePos( char * lpszString ); // 得到在定长表中的位置
bool SetHashTable( char * lpszString ); // 将字符串散列到哈希表中
unsigned long GetTableLength( void );
void SetTableLength( const unsigned long nLength );
~ CHashAlgo()
{
if ( NULL != m_HashIndexTable )
{
delete []m_HashIndexTable;
m_HashIndexTable = NULL;
m_tablelength = 0 ;
}
}
protected :
private :
unsigned long cryptTable[ 0x500 ];
unsigned long m_tablelength; // 哈希索引表长度
MPQHASHTABLE * m_HashIndexTable;
};
二、类实现文件
view plaincopy to clipboardprint ?
/// //
// Name: HashAlgo.cpp
// Purpose: 使用魔兽Hash算法,实现索引表的填充和查找功能。
// Author: 陈相礼
// Modified by:
// Created: 07/30/09
// RCS-ID: $Id: treetest.h 43021 2009-07-30 16:36:51Z VZ $
// Copyright: (C) Copyright 2009, TSong Corporation, All Rights Reserved.
// Licence:
/// //
#include " windows.h "
#include " HashAlgo.h "
//
// 预处理
void CHashAlgo::prepareCryptTable()
{
unsigned long seed = 0x00100001 , index1 = 0 , index2 = 0 , i;
for ( index1 = 0 ; index1 < 0x100 ; index1 ++ )
{
for ( index2 = index1, i = 0 ; i < 5 ; i ++ , index2 += 0x100 )
{
unsigned long temp1, temp2;
seed = (seed * 125 + 3 ) % 0x2AAAAB ;
temp1 = (seed & 0xFFFF ) << 0x10 ;
seed = (seed * 125 + 3 ) % 0x2AAAAB ;
temp2 = (seed & 0xFFFF );
cryptTable[index2] = ( temp1 | temp2 );
}
}
}
//
// 求取哈希值
unsigned long CHashAlgo::HashString( char * lpszFileName, unsigned long dwHashType)
{
unsigned char * key = (unsigned char * )lpszFileName;
unsigned long seed1 = 0x7FED7FED , seed2 = 0xEEEEEEEE ;
int ch;
while ( * key != 0 )
{
ch = toupper( * key ++ );
seed1 = cryptTable[(dwHashType << 8 ) + ch] ^ (seed1 + seed2);
seed2 = ch + seed1 + seed2 + (seed2 << 5 ) + 3 ;
}
return seed1;
}
//
// 得到在定长表中的位置
long CHashAlgo::GetHashTablePos( char * lpszString)
{
const unsigned long HASH_OFFSET = 0 , HASH_A = 1 , HASH_B = 2 ;
unsigned long nHash = HashString(lpszString, HASH_OFFSET);
unsigned long nHashA = HashString(lpszString, HASH_A);
unsigned long nHashB = HashString(lpszString, HASH_B);
unsigned long nHashStart = nHash % m_tablelength,
nHashPos = nHashStart;
while ( m_HashIndexTable[nHashPos].bExists)
{
if (m_HashIndexTable[nHashPos].nHashA == nHashA && m_HashIndexTable[nHashPos].nHashB == nHash)
return nHashPos;
else
nHashPos = (nHashPos + 1 ) % m_tablelength;
if (nHashPos == nHashStart)
break ;
}
return - 1 ; // 没有找到
}
//
// 通过传入字符串,将相应的表项散列到索引表相应位置中去
bool CHashAlgo::SetHashTable( char * lpszString )
{
const unsigned long HASH_OFFSET = 0 , HASH_A = 1 , HASH_B = 2 ;
unsigned long nHash = HashString(lpszString, HASH_OFFSET);
unsigned long nHashA = HashString(lpszString, HASH_A);
unsigned long nHashB = HashString(lpszString, HASH_B);
unsigned long nHashStart = nHash % m_tablelength,
nHashPos = nHashStart;
while ( m_HashIndexTable[nHashPos].bExists)
{
nHashPos = (nHashPos + 1 ) % m_tablelength;
if (nHashPos == nHashStart)
{
#if DEBUGTEST
testid = - 1 ;
#endif
return false ;
}
}
m_HashIndexTable[nHashPos].bExists = true ;
m_HashIndexTable[nHashPos].nHashA = nHashA;
m_HashIndexTable[nHashPos].nHashB = nHash;
strcpy( m_HashIndexTable[nHashPos].test_filename, lpszString );
#if DEBUGTEST
testid = nHashPos;
#endif
return true ;
}
//
// 取得哈希索引表长
unsigned long CHashAlgo::GetTableLength( void )
{
return m_tablelength;
}
//
// 设置哈希索引表长
void CHashAlgo::SetTableLength( const unsigned long nLength )
{
m_tablelength = nLength;
return ;
}
三、测试主文件
view plaincopy to clipboardprint ?
/// //
// Name: DebugMain.cpp
// Purpose: 测试Hash算法封装的类,完成索引表的填充和查找功能的测试。
// Author: 陈相礼
// Modified by:
// Created: 07/30/09
// RCS-ID: $Id: treetest.h 43021 2009-07-30 16:36:51Z VZ $
// Copyright: (C) Copyright 2009, TSong Corporation, All Rights Reserved.
// Licence:
/// //
//
// 测试参数设定宏
#define TESTNUM 32
#include < iostream >
#include < fstream >
#include " HashAlgo.h "
using namespace std;
//
// 测试主函数开始
int main( int argc, char ** argv )
{
CHashAlgo hash_test( TESTNUM );
cout << " 取得初始化散列索引表长为: " << hash_test.GetTableLength() << endl;
bool is_success = hash_test.SetHashTable( " test " );
if ( is_success )
{
cout << " 散列结果一:成功! " << endl;
}
else
{
cout << " 散列结果一:失败! " << endl;
}
is_success = hash_test.SetHashTable( " 测试 " );
if ( is_success )
{
cout << " 散列结果二:成功! " << endl;
}
else
{
cout << " 散列结果二:失败! " << endl;
}
long pos = hash_test.GetHashTablePos( " test " );
cout << " 查找测试字符串:\ " test\ " 的散列位置: " << pos << endl;
pos = hash_test.GetHashTablePos( " 测试 " );
cout << " 查找测试字符串:“测试” 的散列位置: " << pos << endl;
//
// 散列测试
for ( int i = 0 ; i < TESTNUM; i ++ )
{
char buff[ 32 ];
sprintf(buff, " abcdefg%d. " , i);
is_success = hash_test.SetHashTable(buff);
is_success ? cout << buff << " 散列结果:成功!位置: " << hash_test.testid << endl : cout << buff << " 散列结果:失败! " << endl;
}
system( " pause " );
//
// 查找测试
for ( int i = 0 ; i < TESTNUM; i ++ )
{
char buff[ 32 ];
sprintf(buff, " abcdefg%d. " , i);
pos = hash_test.GetHashTablePos( buff );
pos != - 1 ? cout << " 查找测试字符串: " << buff << " 的散列位置: " << pos << endl : cout << buff << " 存在冲突! " << endl;
}
system( " pause " );
return 0 ;
}