mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-01 11:29:51 +08:00
* docs: add Japanese documents (`ja/docs`) * docs: add Japanese documents (`ja/codes`) * docs: add Japanese documents * Remove pythontutor blocks in ja/ * Add an empty at the end of each markdown file. * Add the missing figures (use the English version temporarily). * Add index.md for Japanese version. * Add index.html for Japanese version. * Add missing index.assets * Fix backtracking_algorithm.md for Japanese version. * Add avatar_eltociear.jpg. Fix image links on the Japanese landing page. * Add the Japanese banner. --------- Co-authored-by: krahets <krahets@163.com>
66 lines
1.4 KiB
C++
66 lines
1.4 KiB
C++
/**
|
|
* File: simple_hash.cpp
|
|
* Created Time: 2023-06-21
|
|
* Author: krahets (krahets@163.com)
|
|
*/
|
|
|
|
#include "../utils/common.hpp"
|
|
|
|
/* 加算ハッシュ */
|
|
int addHash(string key) {
|
|
long long hash = 0;
|
|
const int MODULUS = 1000000007;
|
|
for (unsigned char c : key) {
|
|
hash = (hash + (int)c) % MODULUS;
|
|
}
|
|
return (int)hash;
|
|
}
|
|
|
|
/* 乗算ハッシュ */
|
|
int mulHash(string key) {
|
|
long long hash = 0;
|
|
const int MODULUS = 1000000007;
|
|
for (unsigned char c : key) {
|
|
hash = (31 * hash + (int)c) % MODULUS;
|
|
}
|
|
return (int)hash;
|
|
}
|
|
|
|
/* XORハッシュ */
|
|
int xorHash(string key) {
|
|
int hash = 0;
|
|
const int MODULUS = 1000000007;
|
|
for (unsigned char c : key) {
|
|
hash ^= (int)c;
|
|
}
|
|
return hash & MODULUS;
|
|
}
|
|
|
|
/* 回転ハッシュ */
|
|
int rotHash(string key) {
|
|
long long hash = 0;
|
|
const int MODULUS = 1000000007;
|
|
for (unsigned char c : key) {
|
|
hash = ((hash << 4) ^ (hash >> 28) ^ (int)c) % MODULUS;
|
|
}
|
|
return (int)hash;
|
|
}
|
|
|
|
/* ドライバーコード */
|
|
int main() {
|
|
string key = "Hello algorithm";
|
|
|
|
int hash = addHash(key);
|
|
cout << "Additive hash value is " << hash << endl;
|
|
|
|
hash = mulHash(key);
|
|
cout << "Multiplicative hash value is " << hash << endl;
|
|
|
|
hash = xorHash(key);
|
|
cout << "XOR hash value is " << hash << endl;
|
|
|
|
hash = rotHash(key);
|
|
cout << "Rotational hash value is " << hash << endl;
|
|
|
|
return 0;
|
|
} |