-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hash Function.cpp
48 lines (44 loc) · 1.22 KB
/
Hash Function.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;
class Solution {
public:
/**
* @param key: A String you should hash
* @param HASH_SIZE: An integer
* @return an integer
*/
int hashCode(string key,int HASH_SIZE) {
if(key.size() == 0) return 0;
long hashSum = 0;
for(int i = 0; i < key.size(); i++) {
hashSum = 33 * hashSum + key[i];
hashSum %= HASH_SIZE;
}
return (int)hashSum;
}
int hashCode_1(string key,int HASH_SIZE) {
// write your code here
if(key.size() == 0) return 0;
long long hashSum = 0;
for(int i = 0; i < key.size(); i++) {
hashSum += key[i] * solve(33, key.size() - i - 1, HASH_SIZE);
hashSum %= HASH_SIZE;
}
return (int)hashSum;
}
long long solve(int a, int n, int b) {
if(n == 1) return a % b;
else if(n == 0) return 1;
else {
long long product = solve(a, n / 2, b);
product = (product % b) * (product % b) % b;
//cout << "product: " << product << endl;
if(n % 2 == 0) return product;
else return ((product % b) * (a % b)) % b;
}
}
};
int main() {
Solution s;
cout << s.hashCode("Wrong answer or accepted?", 1000000007) << endl;
}