Hash 的核心思想在于,将输入映射到一个值域较小、可以方便比较的范围,典型的用法就是将资源紧张的设备中的不定长字符串转化为定长整数,以达到节省空间的目的

如:printf("This is a string.")  =>  printf("0x12345678")   // 理想哈希算法可将不同的字符串转化为唯一的整数

常用hash算法:

  • 00 - RS 哈希函数
unsigned int RSHash(const char* str, unsigned int length)
{
unsigned int b = 378551;
unsigned int a = 63689;
unsigned int hash = 0;
unsigned int i = 0; for (i = 0; i < length; ++str, ++i)
{
hash = hash * a + (*str);
a = a * b;
} return hash;
}
  • 01 - JS 哈希函数
unsigned int JSHash(const char* str, unsigned int length)
{
unsigned int hash = 1315423911;
unsigned int i = 0; for (i = 0; i < length; ++str, ++i)
{
hash ^= ((hash << 5) + (*str) + (hash >> 2));
} return hash;
}
  • 02 - PJW 哈希函数
unsigned int PJWHash(const char* str, unsigned int length)
{
const unsigned int BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8);
const unsigned int ThreeQuarters = (unsigned int)((BitsInUnsignedInt * 3) / 4);
const unsigned int OneEighth = (unsigned int)(BitsInUnsignedInt / 8);
const unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
unsigned int hash = 0;
unsigned int test = 0;
unsigned int i = 0; for (i = 0; i < length; ++str, ++i)
{
hash = (hash << OneEighth) + (*str); if ((test = hash & HighBits) != 0)
{
hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
} return hash;
}
  • 03 - ELF哈希函数
unsigned int ELFHash(const char* str, unsigned int length)
{
unsigned int hash = 0;
unsigned int x = 0;
unsigned int i = 0; for (i = 0; i < length; ++str, ++i)
{
hash = (hash << 4) + (*str); if ((x = hash & 0xF0000000L) != 0)
{
hash ^= (x >> 24);
} hash &= ~x;
} return hash;
}
  • 04 - BKDR 哈希函数
unsigned int BKDRHash(const char* str, unsigned int length)
{
unsigned int seed = 131; /* 31 131 1313 13131 131313 etc.. */
unsigned int hash = 0;
unsigned int i = 0; for (i = 0; i < length; ++str, ++i)
{
hash = (hash * seed) + (*str);
} return hash;
}
  • 05 - SDBM哈希函数
unsigned int SDBMHash(const char* str, unsigned int length)
{
unsigned int hash = 0;
unsigned int i = 0; for (i = 0; i < length; ++str, ++i)
{
hash = (*str) + (hash << 6) + (hash << 16) - hash;
} return hash;
}
  • 06 - DJB哈希函数
unsigned int DJBHash(const char* str, unsigned int length)
{
unsigned int hash = 5381;
unsigned int i = 0; for (i = 0; i < length; ++str, ++i)
{
hash = ((hash << 5) + hash) + (*str);
} return hash;
}
  • 07 - DEK哈希函数
unsigned int DEKHash(const char* str, unsigned int length)
{
unsigned int hash = len;
unsigned int i = 0; for (i = 0; i < length; ++str, ++i)
{
hash = ((hash << 5) ^ (hash >> 27)) ^ (*str);
} return hash;
}
  • 08 - AP 哈希函数
unsigned int APHash(const char* str, unsigned int length)
{
unsigned int hash = 0xAAAAAAAA;
unsigned int i = 0; for (i = 0; i < length; ++str, ++i)
{
hash ^= ((i & 1) == 0) ? ( (hash << 7) ^ (*str) * (hash >> 3)) :
(~((hash << 11) + ((*str) ^ (hash >> 5))));
} return hash;
}

各hash算法冲突率测评

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效果最差,但得分相似,其算法本质是相似的。

参考:

http://www.partow.net/programming/hashfunctions/

    https://www.cnblogs.com/uvsjoh/archive/2012/03/27/2420120.html

【Hash】字符串哈希的更多相关文章

  1. HASH 字符串哈希 映射转化

    哈希HASH的本质思想类似于映射.离散化. 哈希,通过给不同字符赋不同的值.并且钦定一个进制K和模数,从而实现一个字符串到一个模意义下的K进制数上. 它的主要目的是判重,用于$DFS$.$BFS$判重 ...

  2. 详解HASH(字符串哈希)

    HASH意为(散列),是OI的常用算法. 我们常用哈希的原因是,hash可以快速(一般来说是O(段长))的求出一个子段的hash值,然后就可以快速的判断两个串是否相同. 今天先讲string类的has ...

  3. 数据结构作业——hash(字符串哈希)

    Hash Description 给定长度为 n ( n<=1000000)的字符串,字符串仅由小写字母的前 m ( m<=6) 个字符组成,请你计算出共有多少长度为 k( k<=6 ...

  4. 从Hash Killer I、II、III论字符串哈希

    首先,Hash Killer I.II.III是BZOJ上面三道很经典的字符串哈希破解题.当时关于II,本人还琢磨了好久,但一直不明白为啥别人AC的代码都才0.3kb左右,直到CYG神犇说可以直接随机 ...

  5. Redis支持的数据类型及相应操作命令:String(字符串),Hash(哈希),List(列表),Set(集合)及zset(sorted set:有序集合)

    help 命令,3种形式: help 命令 形式 help @<group> 比如:help @generic.help @string.help @hash.help @list.hel ...

  6. 字符串哈希hash

    题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的想好好练习哈希的话,请自觉,否则请右转 ...

  7. 【基本算法入门-字符串哈希(Hash)】-C++

    字符串哈希入门 说得通俗一点,字符串哈希实质上就是把每个不同的字符串转成不同的整数. 为什么会有这样的需要呢?很明显,存储一个超长的字符串和存储一个超大但是能存的下的整数,后者所占的空间会少的多,但主 ...

  8. Crazy Search POJ - 1200 (字符串哈希hash)

    Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could ...

  9. 牛客练习赛33 E tokitsukaze and Similar String (字符串哈希hash)

    链接:https://ac.nowcoder.com/acm/contest/308/E 来源:牛客网 tokitsukaze and Similar String 时间限制:C/C++ 2秒,其他语 ...

  10. luoguP3370 【模板】字符串哈希 [hash]

    题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的想好好练习哈希的话,请自觉,否则请右转 ...

随机推荐

  1. 中企网安信息科技:数据防泄密DLP管理系统概述

    由华企网安总公司北京中企网安信息科技有限责任公司开发的<数据防泄密DLP管理系统>,获得国家版权局颁发的计算机软件著作权登记证书. 数据防泄密DLP管理系统是用于保护.监控和管理敏感数据的 ...

  2. springboot整合mybatis步骤思路

    /** * springboot整合mybatis步骤思路 * 依赖导入 * 建表 * 实体类 * mapper配置文件 * mapper接口 * yaml配置 * properties配置数据库连接 ...

  3. Unity无法显示animator面板,如何解决?

    步骤: 点击动画的主体: 右侧Inspector面板找到Animator,双击Controller中的对象: 左上角即可显示animator面板. 总结: 不行就双击!!!!!!!!!!!!!!!!! ...

  4. javaScript正则截取自定义标签-javascript-zheng-ze-jie-qu-zi-ding-yi-biao-qian

    title: javaScript正则截取自定义标签 date: 2021-12-29 17:31:48.448 updated: 2021-12-29 17:31:48.448 url: https ...

  5. Python图像处理丨图像缩放、旋转、翻转与图像平移

    摘要:本篇文章主要讲解Python调用OpenCV实现图像位移操作.旋转和翻转效果,包括四部分知识:图像缩放.图像旋转.图像翻转.图像平移. 本文分享自华为云社区<[Python图像处理] 六. ...

  6. 6个步骤强化 CI/CD 安全

    快速的数字化和越来越多的远程业务运营给开发人员带来了沉重的负担,他们不断面临着更快推出软件的压力.尽管CI/CD 加速了产品发布,但它容易受到网络安全问题的影响,例如代码损坏.安全配置错误和机密管理不 ...

  7. 火山引擎DataLeap的Catalog系统搜索实践 (二):整体架构

    整体架构 火山引擎DataLeap的Catalog搜索系统使用了开源的搜索引擎Elasticsearch进行基础的文档检索(Recall阶段),因此各种资产元数据会被存放到Elasticsearch中 ...

  8. Spring Boot CMD 运行日志输出中文乱码

    Spring Boot 在Windows CMD 中运行,日志输出中文乱码name="CONSOLE" 设置成 charset utf-8 ,在windows cmd 中运行时,l ...

  9. 如何安装和使用 Hugging Face Unity API

    Hugging Face Unity API 提供了一个简单易用的接口,允许开发者在自己的 Unity 项目中方便地访问和使用 Hugging Face AI 模型,已集成到 Hugging Face ...

  10. SpringMVC 获取请求参数(精简版)

    SpringMVC获取请求数据: DispatcherServlet底层中获取请求数据并传递给单元方法使用. DispatcherServlet会根据请求动态调用对应的单元方法处理,而请求先被Disp ...