Lintcode: Hash Function && Summary: Modular Multiplication, Addition, Power && Summary: 长整形long

In data structure Hash, hash function is used to convert a string(or any other type) into an integer smaller than hash size and bigger or equal to zero. The objective of designing a hash function is to "hash" the key as unreasonable as possible. A good hash function can avoid collision as less as possible. A widely used hash function algorithm is using a magic number 33, consider any string as a 33 based big integer like follow:

hashcode("abcd") = (ascii(a) * 333 + ascii(b) * 332 + ascii(c) *33 + ascii(d)) % HASH_SIZE 

                              = (97* 33^3 + 98 * 33^2 + 99 * 33 +100) % HASH_SIZE

                              = 3595978 % HASH_SIZE

here HASH_SIZE is the capacity of the hash table (you can assume a hash table is like an array with index 0 ~ HASH_SIZE-1).

Given a string as a key and the size of hash table, return the hash value of this key.f

Example
For key="abcd" and size=100, return 78

Clarification
For this problem, you are not necessary to design your own hash algorithm or consider any collision issue, you just need to implement the algorithm as described.

对于overflow要特别小心,即便是intermediate result,也要小心overflow

Fast Power那道题类似,modular multiplication addition的证明在此

    • (a + b) % p = (a % p + b % p) % p (1)
    • (a - b) % p = (a % p - b % p) % p (2)
    • (a * b) % p = (a % p * b % p) % p (3)
    • a ^ b % p = ((a % p)^b) % p (4)
 1 class Solution {
 2     /**
 3      * @param key: A String you should hash
 4      * @param HASH_SIZE: An integer
 5      * @return an integer
 6      */
 7     public int hashCode(char[] key,int HASH_SIZE) {
 8         if (key.length == 0) return 0;
 9         int result = 0;
10         int base = 1;
11         for (int i=key.length-1; i>=0; i--) {
12             int num = (int)(key[i] - ‘\0‘);
13             result += modMultiply(num, base, HASH_SIZE);
14             result = result % HASH_SIZE;
15             base = modMultiply(base, 33, HASH_SIZE);
16         }
17         return result % HASH_SIZE;
18     }
19
20     public int modMultiply(int a, int b, int c) {
21         long temp = (long)a * b;
22         return (int)(temp % c);
23     }
24 };

15行如果如果每次都 base *= 33,即使把base 定义为long型,String一长也会溢出的; 我又试了用Math.pow(33, i), 一样overflow。说明强行算33的power还是不行的,是会溢出的。还是要用Modular Multiplication

(A * B) mod C, 需要注意的是,A、B、C都定义为int,A*B要小心overflow, 所以用long来存这个乘积,然后mod C之后再把结果存成int型

第21行,如果是就是错的:a * b已经overflow了

1 public int modMultiply(int a, int b, int c) {
2          long temp = a * b;
3          return (int)(temp % c);
4      }
5 };

处理方法可以是:long temp = (long) a * b;  或者a和b都定义为long型然后long temp = a * b

方法二:不用long type, 网上别人的做法,没看懂

 1     public int hashCode(char[] key,int HASH_SIZE) {
 2         int result = 0;
 3         for (int i = 0; i < key.length; i++) {
 4             result = helper(result, 33, HASH_SIZE);
 5             result += key[i];
 6             result %= HASH_SIZE;
 7         }
 8         return result;
 9     }
10
11     int helper(int num, int base, int mod) {
12         int result = 0;
13         int temp = num - mod;
14         for (int i = 0; i < base; i++) {
15             if (result + temp > 0) {
16                 result += temp;
17             } else {
18                 result += num;
19             }
20         }
21         return result;
22     }
时间: 2024-10-05 09:13:12

Lintcode: Hash Function && Summary: Modular Multiplication, Addition, Power && Summary: 长整形long的相关文章

Hash function

Hash function From Wikipedia, the free encyclopedia A hash function that maps names to integers from 0 to 15. There is a collision between keys "John Smith" and "Sandra Dee". A hash function is any function that maps data of arbitrary

General Purpose Hash Function Algorithms

General Purpose Hash Function Algorithms [email protected]: http://www.partow.net/programming/hashfunctions/index.html     Description Hashing Methodologies Hash Functions and Prime Numbers Bit Biases Various Forms Of Hashing String Hashing Cryptogra

POJ1060 Modular multiplication of polynomials解题报告 (2011-12-09 20:27:53)(转)

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3239   Accepted: 1459 Consider polynomials whose coefficients are 0 and 1. Addition of two polynomials is achieved by 'adding' the coefficients for the corresponding powers in the polynomia

POJ 1060 Modular multiplication of polynomials 逻辑运算

Modular multiplication of polynomials Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4289   Accepted: 1944 Description Consider polynomials whose coefficients are 0 and 1. Addition of two polynomials is achieved by 'adding' the coeffici

hash function比较

http://blog.csdn.net/kingstar158/article/details/8028635 由于工作需要,针对千万级别的数据,使用stl::map着实存在着效率问题,最后使用boost::unordered_map替代前者,发现效率上有很大的提升,但是还是无法达到我们的需求: stl::map  底层算法:B+tree 实现 boost::unordered_map 底层算法:hash 实现 所以可能要针对不同的数据类型编写hash function来优化查找和插入的效率,

lintcode 容易题:Hash Function 哈希函数

题目: 哈希函数 在数据结构中,哈希函数是用来将一个字符串(或任何其他类型)转化为小于哈希表大小且大于等于零的整数.一个好的哈希函数可以尽可能少地产生冲突.一种广泛使用的哈希函数算法是使用数值33,假设任何字符串都是基于33的一个大整数,比如: hashcode("abcd") = (ascii(a) * 333 + ascii(b) * 332 + ascii(c) *33 + ascii(d)) % HASH_SIZE = (97* 333 + 98 * 332 + 99 * 33

hash function 3种方法 1不好 2一般 3好

1. h(k) =  k mod m its is really bad in the practical. if m = even and k is all even.... ( m is size of hash table, modulo ['m?djul?u ) 2. multiplication method. 好一点 a multiplice the k and sum mod 2^w  w is the bit length integer.  two power of two.

N多校2018d3t10 Hash Function

理解hash过程 首先可知,先放进去肯定是h[i]%n==i的位置,这些个位置放进set按照字典序顺序来,然后每安排一个这样点,对于它后一位的值,如果还没被放入set,则如果它%对应的位置已经放入set了则它放入set ~ ac代码: #include <bits/stdc++.h> using namespace std; #define per(i,a,b) for(int i=a;i<b;i++) #define MP make_pair typedef pair<int,i

poj 1060 Modular multiplication of polynomials

方法(无证明,lz弱渣请谅解): 以样例来讲:(x^6 + x^4 + x^2 + x + 1) (x^7 + x + 1) modulo (x^8 + x^4 + x^3 + x + 1) = x^7 + x^6 + 1 . (x^6 + x^4 + x^2 + x + 1) (x^7 + x + 1) =x^13 + x^11 + x^9 + x^8 + x^6 + x^5 + x^4 + x^3 + 1 . 令a=x^13 + x^11 + x^9 + x^8 + x^6 + x^5 +