8个常用的字符串哈希函数

简介: 最有用的两个 unsigned long long hash; // hash使用unsigned long long,超过了unsigned long long就相当于取模//常用11 SDBMHashint SDBMHash(...

最有用的两个

unsigned long long hash; 
// hash使用unsigned long long,超过了unsigned long long就相当于取模
//常用1
1  SDBMHash
int SDBMHash(char *str)
{
    hash = 0;
    while (*str) hash = (*str++) + (hash << 6) + (hash << 16) - hash;
    return hash;
}
//常用2 
int BKDRHash(char *str)
{
    int seed = 131; 
    int hash = 0;
    while (*str) hash = hash * seed + (*str++);
    return hash;
}



常见的8个

const int MAXN = 1000003;

//常用 1
1  SDBMHash
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)%MAXN;
}
 
2  RS Hash Function
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)%MAXN;
}
 
3 JS Hash Function
unsigned int JSHash(char *str)
{
    unsigned int hash = 1315423911;
 
    while (*str)
    {
        hash ^= ((hash << 5) + (*str++) + (hash >> 2));
    }
    return (hash & 0x7FFFFFFF)%MAXN;
}
 
4 P. J. Weinberger Hash Function
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)%MAXN;
}
 
5 ELF Hash Function
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)%MAXN;
}
 
//常用2 
6 BKDR Hash Function
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)%MAXN;
}
 
7 DJB Hash Function
unsigned int DJBHash(char *str)
{
    unsigned int hash = 5381;
    while (*str)
    {
        hash += (hash << 5) + (*str++);
    }
    return (hash & 0x7FFFFFFF)%MAXN;
}
 
8 AP Hash Function
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)%MAXN;
}


目录
相关文章
|
7月前
|
机器学习/深度学习 C语言
函数递归与迭代附n的阶乘+顺序打印一个整数的每一位数+求第n个斐波那契数
函数递归与迭代附n的阶乘+顺序打印一个整数的每一位数+求第n个斐波那契数
54 0
|
7月前
|
算法
【算法总结】字符串哈希
【算法总结】字符串哈希
105 0
|
存储 算法 安全
算法训练Day9| 28. 实现 strStr();459.重复的子字符串;字符串总结 ;双指针回顾
算法训练Day9| 28. 实现 strStr();459.重复的子字符串;字符串总结 ;双指针回顾
|
C语言 容器
互换函数和反转字符串
互换函数和反转字符串
105 0
字符串哈希
原题链接841. 字符串哈希 - AcWing题库 视频讲解AcWing 841. 字符串哈希 - AcWing
77 0
|
存储 算法 Java
算法打卡Day27_leetcode _415字符串相加
算法打卡Day27_leetcode _415字符串相加
算法打卡Day27_leetcode _415字符串相加
|
C语言 C++ 容器
算法库-全排列,递归全排列 auti()字符转为整型 stringstream用法
算法库-全排列,递归全排列 auti()字符转为整型 stringstream用法
算法库-全排列,递归全排列 auti()字符转为整型 stringstream用法
|
算法
LeetCode算法:求出字符串的最大回文子串 及 长度【只利用字符串反转就可】
LeetCode算法:求出字符串的最大回文子串 及 长度【只利用字符串反转就可】
85 0
|
机器学习/深度学习 存储 容器
1044. 最长重复子串 :「字符串哈希 + 二分」&「后缀数组」
1044. 最长重复子串 :「字符串哈希 + 二分」&「后缀数组」
|
机器学习/深度学习 算法 测试技术
686. 重复叠加字符串匹配 :「卡常」&「上下界性质」&「KMP」&「字符串哈希」
686. 重复叠加字符串匹配 :「卡常」&「上下界性质」&「KMP」&「字符串哈希」