整数及字符串哈希
来源:互联网 发布:岭回归 数据标准化 编辑:程序博客网 时间:2024/06/05 23:42
Hash
此处哈希指直接映射,以空间换时间,提供几乎O( 1 )的查询速度。
PS: hash数组用前,请memset(hash,-1,sizeof(hash));
1:整数哈希,数字可重复
const int maxn = 0xff;int hash[maxn], count[maxn]; int hashit(int k){ int t = k % maxn; if( t < 0 )t += maxn; while( hash[t] != -1 && hash[t] != k )t = (t + 1) % maxn; if( hash[t] == -1 )hash[t] = k, count[t] = 1;else ++count[t];}
2:字符串哈希(一般可用字典树代替)
const int maxn = 0xff;int hash[maxn], count[maxn];void init(){ memset(hash,-1,sizeof(hash)); memset(count,-1,sizeof(count));}inline 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);}inline void hashit(char *str){ int k,t; while( *str == '0' ) str++; ///这里有时候不需要去零 k = BKDRHash(str); t = k % maxn; while( hash[t] != k && hash[t] != -1 )t = ( t + 10 ) % maxn; if( hash[t] == -1 ) hash[t] = k, count[t] = 1; else ++count[t];}
3:常用的字符串哈希函数
// RS Hash Functionunsigned 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 Functionunsigned int JSHash(char* str){ unsigned int hash = 1315423911 ; while (*str) { hash ^= ((hash << 5 ) + (*str ++ ) + (hash >> 2 )); } return (hash & 0x7FFFFFFF );}// P. J. Weinberger Hash Functionunsigned 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 Functionunsigned 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 Functionunsigned 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 );}// SDBM Hash Functionunsigned int SDBMHash(char* str){ unsigned int hash = 0 ; while (*str) { hash = (*str ++ ) + (hash << 6 ) + (hash << 16 ) - hash; } return (hash & 0x7FFFFFFF );}// DJB Hash Functionunsigned int DJBHash(char* str){ unsigned int hash = 5381 ; while (*str) { hash += (hash << 5 ) + (*str ++ ); } return (hash & 0x7FFFFFFF );}// AP Hash Functionunsigned 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 );}
- 整数及字符串哈希
- 字符串转换整数及整数转换字符串
- 字符串转化为整数的算法改进及优化
- 字符串转化为整数的算法改进及优化
- 整数转化为整数字符串
- 【字符串处理算法】字符串转换为整数的算法设计及C代码实现
- 字符串->整数atoi,整数->字符串itoa
- 整数变字符串 字符串变整数
- 字符串代替、字符串转换整数
- 字符串处理------字符串转整数
- 【字符串】字符串转为整数(atoi)
- 字符串转成整数
- 字符串转换为整数
- 整数转换成字符串
- 整数转化为字符串
- 整数字符串转化
- 字符串转换为整数
- 整数字符串的转换
- oracle数据库中字段别名使用查询多表
- CComboBox排序
- watir鼠标点击事件
- Execute a system command using difference languages
- 权限管理模块设计
- 整数及字符串哈希
- Android自定义View
- java单元测试 java.lang.OutOfMemoryError: Java heap space
- unicode GB2312转换
- pku 1753 Flip Game(高斯消元+枚举)
- 让一些游戏变成全屏
- 一步步DIY: OSM-Web服务器(一) PostgreSql 配置以及osm2pgsql原始PBF数据导入
- bsearch(二元搜索)
- cd 函数