mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-17 07:38:23 +08:00
Add ru version (#1865)
* Add Russian docs site baseline * Add Russian localized codebase * Polish Russian code wording * Update ru code translation. * Update code translation and chapter covers. * Fix pythontutor extraction. * Add README and landing page. * placeholder of profiles * Use figures of English version * Remove chapter paperbook
This commit is contained in:
66
ru/codes/cpp/chapter_hashing/simple_hash.cpp
Normal file
66
ru/codes/cpp/chapter_hashing/simple_hash.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* 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 Algo";
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user