字符串Hash模板

 1 unsigned int SDBMHash(char *str) {
 2     unsigned int hash = 0;
 3     while (*str)
 4         // equivalent to: hash = 65599*hash + (*str++);
 5         hash = (*str++) + (hash << 6) + (hash << 16) - hash;
 6
 7     return (hash & 0x7FFFFFFF);
 8 }
 9
10 // RS Hash Function
11 unsigned int RSHash(char *str) {
12     unsigned int b = 378551;
13     unsigned int a = 63689;
14     unsigned int hash = 0;
15
16     while (*str) {
17         hash = hash * a + (*str++);
18         a *= b;
19     }
20     return (hash & 0x7FFFFFFF);
21 }
22
23 // JS Hash Function
24 unsigned int JSHash(char *str) {
25     unsigned int hash = 1315423911;
26
27     while (*str)
28         hash ^= ((hash << 5) + (*str++) + (hash >> 2));
29
30     return (hash & 0x7FFFFFFF);
31 }
32
33 // P. J. Weinberger Hash Function
34 unsigned int PJWHash(char *str) {
35     unsigned int BitsInUnignedInt = (unsigned int) (sizeof(unsigned int) * 8);
36     unsigned int ThreeQuarters    = (unsigned int) ((BitsInUnignedInt  * 3) / 4);
37     unsigned int OneEighth        = (unsigned int) (BitsInUnignedInt / 8);
38     unsigned int HighBits         = (unsigned int) (0xFFFFFFFF) << (BitsInUnignedInt - OneEighth);
39     unsigned int hash             = 0;
40     unsigned int test             = 0;
41
42     while (*str) {
43         hash = (hash << OneEighth) + (*str++);
44         if ((test = hash & HighBits) != 0)
45             hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
46     }
47     return (hash & 0x7FFFFFFF);
48 }
49
50 // ELF Hash Function
51 unsigned int ELFHash(char *str) {
52     unsigned int hash = 0;
53     unsigned int x    = 0;
54
55     while (*str) {
56         hash = (hash << 4) + (*str++);
57         if ((x = hash & 0xF0000000L) != 0) {
58             hash ^= (x >> 24);
59             hash &= ~x;
60         }
61     }
62
63     return (hash & 0x7FFFFFFF);
64 }
65
66 // BKDR Hash Function
67 unsigned int BKDRHash(char *str) {
68     unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
69     unsigned int hash = 0;
70
71     while (*str)
72         hash = hash * seed + (*str++);
73
74     return (hash & 0x7FFFFFFF);
75 }
76
77 // DJB Hash Function
78 unsigned int DJBHash(char *str) {
79     unsigned int hash = 5381;
80
81     while (*str)
82         hash += (hash << 5) + (*str++);
83
84     return (hash & 0x7FFFFFFF);
85 }
86
87 // AP Hash Function
88 unsigned int APHash(char *str) {
89     unsigned int hash = 0;
90
91     for (int i = 0; *str; ++i)
92         if ((i & 1) == 0)
93             hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3));
94         else
95             hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5)));
96
97     return (hash & 0x7FFFFFFF);
98 }
时间: 2024-08-04 17:18:40

字符串Hash模板的相关文章

字符串HASH模板 取自挑战程序设计竞赛(第2版)

/*===================================================* 从b串中寻找和a串长度相同的子串,返回开始位置 不保证绝对正确,发生冲突概率为O(sqrt(n)), n为哈希函数的最大值 \*===================================================*/ #define ull unsigned long long const ull B = 1e8+7; /*according to the book*/

poj 3461 字符串单串匹配--KMP或者字符串HASH

http://poj.org/problem?id=3461 先来一发KMP算法: #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <iostream> #include <cmath> #include <map> #include <queue> using namespace std;

LA 4513 Stammering Aliens 字符串hash

字符串hash模板, 本题是求,给定字符串s中至少出现m次的最长字符串长度,及此时起始位置的最大值 LA 4513  Stammering Aliens 白书p225 //#pragma warning (disable: 4786) //#pragma comment (linker, "/STACK:16777216") //HEAD #include <cstdio> #include <ctime> #include <cstdlib> #i

cf244D. Match &amp; Catch 字符串hash (模板)或 后缀数组。。。

D. Match & Catch 可以用各种方法做,字符串hash,后缀数组,dp,拓展kmp,字典树... 字符串hash(模板) http://blog.csdn.net/gdujian0119/article/details/6777239 BKDR Hash Function : // BKDR Hash Function unsigned int BKDRHash(char *str) { unsigned int seed = 131; // 31 131 1313 13131 13

cf244D. Match &amp;amp; Catch 字符串hash (模板)或 后缀数组。。。

D. Match & Catch 能够用各种方法做.字符串hash.后缀数组,dp.拓展kmp,字典树.. . 字符串hash(模板) http://blog.csdn.net/gdujian0119/article/details/6777239 BKDR Hash Function : // BKDR Hash Function unsigned int BKDRHash(char *str) { unsigned int seed = 131; // 31 131 1313 13131 1

hash 模板

首先,附上hash完整模板,分析见:http://www.cnblogs.com/wsdestdq/p/6831041.html 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 6 typedef long long LL; 7 8 int n; 9 const int D = 131; 10 const int MD = 1e9 + 7; 11 i

hash 模板 x

不懂hash是什么的盆友给出直通车:滴滴滴,开车啦~ 如果你看懂了的话: hash模板来也~ #include <cstdio> #include <string> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int N = 1e5 + 6; //10006 const int D = 131; //质数 const int

Codeforces 898F 字符串hash

F. Restoring the Expression 题意:给出一个字符串,要把它折分成三部分 a.b.c , 使得 a+b=c .输出任何一种可行情况. tags:字符串 hash 因为 a+b=c ,所以 lena.lenb 至少要有一个等于 lenc 或 lenc-1  .所以枚举 lenc,每次检验一下可不可行. 但每次都暴力检验肯定超时,这里把字符串hash 一下,根据 hash值 快速检验. 记一下模板: const int mod = 1e9+7; ll Hash[N], p[N

字符串hash与字典树

title: 字符串hash与字典树 date: 2018-08-01 22:05:29 tags: acm 算法 字符串 概述 这篇主要是关于字符串里的 字符串hash 和 字符串字典树,,两个都是简单的套模板的东西,,,理解基本思想就行了,,,对了,,还有一个字典树的的变形--01字典树: 字符串hash 如何求一个字符串的hash值 字符串hash的作用就是将 字符串有效的转化为一个整数 ,,这个转化过程利用的是一个 hash函数 例如,,我们选hash函数为 \(hash[i]=(has