64,654
社区成员
发帖
与我相关
我的任务
分享
#include <stdint.h>
// 假定单词都是小写的
uint64_t exp_i(int base, int scale)
{
uint64_t result = 1;
for (int i=0; i<scale; i++) result *= base;
return result;
}
uint64_t word_hash(const char* str)
{
uint64_t result = 0;
for (int i=0; str[i]!=0; i++) {
int num = str[i]-'a';
result += num * exp_i(26,i);
}
return result;
}
unsigned int SDBMHash( char * str)
{
unsigned int hash = 0 ;
while ( * str)
{
hash = ( * str ++ ) + (hash << 6 ) + (hash << 16 ) - hash;
}
return (hash & 0x7FFFFFFF );
}