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) * 33^3 + ascii(b) * 33^2 + 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)
 class Solution {
     /**
      * @param key: A String you should hash
      * @param HASH_SIZE: An integer
      * @return an integer
      */
     public int hashCode(char[] key,int HASH_SIZE) {
         if (key.length == 0) return 0;
         int result = 0;
         int base = 1;
         for (int i=key.length-1; i>=0; i--) {
             int num = (int)(key[i] - '\0');
             result += modMultiply(num, base, HASH_SIZE);
             result = result % HASH_SIZE;
             base = modMultiply(base, 33, HASH_SIZE);
         }
         return result % HASH_SIZE;
     }
     public int modMultiply(int a, int b, int c) {
         long temp = (long)a * b;
         return (int)(temp % c);
     }
 };
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了
 public int modMultiply(int a, int b, int c) {
          long temp = a * b;
          return (int)(temp % c);
      }
 };
处理方法可以是:long temp = (long) a * b; 或者a和b都定义为long型然后long temp = a * b
方法二:不用long type, 网上别人的做法,没看懂
     public int hashCode(char[] key,int HASH_SIZE) {
         int result = 0;
         for (int i = 0; i < key.length; i++) {
             result = helper(result, 33, HASH_SIZE);
             result += key[i];
             result %= HASH_SIZE;
         }
         return result;
     }
     int helper(int num, int base, int mod) {
         int result = 0;
         int temp = num - mod;
         for (int i = 0; i < base; i++) {
             if (result + temp > 0) {
                 result += temp;
             } else {
                 result += num;
             }
         }
         return result;
     }
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 fr ... 
- General Purpose Hash Function Algorithms
		General Purpose Hash Function Algorithms post@: http://www.partow.net/programming/hashfunctions/inde ... 
- POJ1060 Modular multiplication of polynomials解题报告 (2011-12-09 20:27:53)
		Modular multiplication of polynomials Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3 ... 
- POJ 1060:Modular multiplication of polynomials
		Modular multiplication of polynomials Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4 ... 
- STL标准库-一个万用的hash function
		技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 在前面我介绍过hash的使用,本次主要介绍一下Hash Function Hash Function即获得hash code的函 ... 
- hash function比较
		http://blog.csdn.net/kingstar158/article/details/8028635 由于工作需要,针对千万级别的数据,使用stl::map着实存在着效率问题,最后使用bo ... 
- POJ1060 Modular multiplication of polynomials
		题目来源:http://poj.org/problem?id=1060 题目大意: 考虑系数为0和1的多项式.两个多项式的加法可以通过把相应次数项的系数相加而实现.但此处我们用模2加法来计算系数之和. ... 
- You shouldn't use *any* general-purpose hash function for user passwords, not BLAKE2, and not MD5, SHA-1, SHA-256, or SHA-3
		hashlib - Secure hashes and message digests - Python 3.8.3 documentation https://docs.python.org/3.8 ... 
- 常用加密算法学习总结之散列函数(hash function)
		散列函数(Hash function)又称散列算法.哈希函数,散列函数把消息或数据压缩成摘要,使得数据量变小,将数据的格式固定下来.该函数将数据打乱混合,重新创建一个叫做散列值(hash values ... 
随机推荐
- C# 给主程序签名及第三方dll强签名操作
			1.给主程序添加签名 添加完成后会自动生成一个*.pfx文件. 2.给第三方程序添加强签名方法: 本文以WAPIWrapperCSharp.dll为例,使用vs Tools下的工具命令 ... 
- 【CF739E】Gosha is hunting 贪心
			[CF739E]Gosha is hunting 题意:有n个小精灵,你有a个普通球和b个超级球,用普通球抓住第i只小精灵的概率为$A_i$,用超级球抓住第i只小精灵的概率为$u_i$.你必须一开始就 ... 
- python nose测试框架全面介绍六--框架函数别名
			之前python nose测试框架全面介绍二中介绍了nose框架的基本构成,但在实际应该中我们也会到setup_function等一系列的名字,查看管网后,我们罗列下nose框架中函数的别名 1.pa ... 
- Run-Time Check Failure #2  Stack around the variable ‘xxx’ was corrupted
			在改别人代码时,运行报错: Run-Time Check Failure #2 Stack around the variable 'buffer' was corrupted 这表明你对某变量的赋值 ... 
- backbone.js之Model篇 简单总结和深入(2)
			一.模型属性的一些操作方法 1.mmodel.get() 获取属性的值 2.mmodel.set('age',5) 更新单个属性的值 mmodel.set({name:'aaa',age:6}) ... 
- python的接口和抽象类
			抽象基类 有些面向对象的语言,如JAVA,支持接口,可以声明一个支持给定的一些方法方法,或者支持给定存取协议的类.抽象基类(或者ABCs)是Python里一个相同的特性.抽象基类由abc模块构成,包含 ... 
- java.exe和javaw.exe有什么区别
- ububtu16.04下安装protobuf
			重新下载protobuf,我下载的时最新的protobuf-all-3.5.1.tar.gz protobuf网址:https://github.com/google/protobuf/relea ... 
- 计蒜客 31460 - Ryuji doesn't want to study - [线段树][2018ICPC徐州网络预赛H题]
			题目链接:https://nanti.jisuanke.com/t/31460 Ryuji is not a good student, and he doesn't want to study. B ... 
- CodeForces - 156B  Suspects 逻辑 线性 想法 题
			题意:有1~N,n(1e5)个嫌疑人,有m个人说真话,每个人的陈述都形如X是凶手,或X不是凶手.现在给出n,m及n个陈述(以+x/-X表示)要求输出每个人说的话是true ,false or notd ... 
