/
githubmirror
/
hello-algo
Обзор
Документация
Войти
/
githubmirror
/
hello-algo
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
ja/codes/cpp/chapter_hashing/simple_hash.cpp
66 строк
1 KB
Yudong Jin
Re-translate the Japanese version (#1871)
30 мар 2026, 02:30
Не верифицирован
30 мар 2026, 02:30
d7b2277
Код
Авторство
О чём код?
/** * 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; } /* Driver Code */ int main() { string key = "Hello アルゴリズム"; int hash = addHash(key); cout << "加算ハッシュ値は " << hash << endl; hash = mulHash(key); cout << "乗算ハッシュ値は " << hash << endl; hash = xorHash(key); cout << "XORハッシュ値は " << hash << endl; hash = rotHash(key); cout << "回転ハッシュ値は " << hash << endl; return 0; }