Files
Yudong Jin 772183705e 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
2026-03-28 04:24:07 +08:00

67 lines
1.7 KiB
C#

/**
* File: simple_hash.cs
* Created Time: 2023-06-26
* Author: hpstory (hpstory1024@163.com)
*/
namespace hello_algo.chapter_hashing;
public class simple_hash {
/* Аддитивное хеширование */
int AddHash(string key) {
long hash = 0;
const int MODULUS = 1000000007;
foreach (char c in key) {
hash = (hash + c) % MODULUS;
}
return (int)hash;
}
/* Мультипликативное хеширование */
int MulHash(string key) {
long hash = 0;
const int MODULUS = 1000000007;
foreach (char c in key) {
hash = (31 * hash + c) % MODULUS;
}
return (int)hash;
}
/* XOR-хеширование */
int XorHash(string key) {
int hash = 0;
const int MODULUS = 1000000007;
foreach (char c in key) {
hash ^= c;
}
return hash & MODULUS;
}
/* Хеширование с циклическим сдвигом */
int RotHash(string key) {
long hash = 0;
const int MODULUS = 1000000007;
foreach (char c in key) {
hash = ((hash << 4) ^ (hash >> 28) ^ c) % MODULUS;
}
return (int)hash;
}
[Test]
public void Test() {
string key = "Hello Algo";
int hash = AddHash(key);
Console.WriteLine("Хеш-сумма сложением = " + hash);
hash = MulHash(key);
Console.WriteLine("Хеш-сумма умножением = " + hash);
hash = XorHash(key);
Console.WriteLine("Хеш-сумма XOR = " + hash);
hash = RotHash(key);
Console.WriteLine("Хеш-сумма с циклическим сдвигом = " + hash);
}
}