字符串的hash函数:
(1)
unsigned int SDBMHash(char *str)
{
unsigned int hash = 0;
while (*str)
{
// 等价于: hash = 65599*hash + (*str++);
hash = (*str++) + (hash << 6) + (hash << 16) - hash;
}
return (hash & 0x7FFFFFFF);
}
(2)
//用跟元素个数最接近的质数作为散列表的大小
#define NHASH 29989
#define MULT 31unsigned in hash(char *p)
{
unsigned int h = 0;
for (; *p; p++)
h = MULT *h + *p;
return h % NHASH;
}
(3)
int hash(string i)标签:hash,函数,int,unsigned,str,ans,字符串,return From: https://blog.51cto.com/u_15911260/5934651
{
int l=i.length, s=7,ans=0,t=9999997;
for (int q=0;q<l;q++)
{
ans+=i[q]*s;
s*=7;
ans=ans%t;//在这里面%t,主要是为了防止数据溢出。
}
return ans;
}
//字符串里面的每一个数 比如字符串abc 写成 a*7^1+b*7^2+c*7^3 然后再取模 .