mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +08:00
docs: add Japanese translate documents (#1812)
* 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>
This commit is contained in:
committed by
GitHub
parent
2487a27036
commit
954c45864b
110
ja/codes/cpp/chapter_hashing/array_hash_map.cpp
Normal file
110
ja/codes/cpp/chapter_hashing/array_hash_map.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* File: array_hash_map.cpp
|
||||
* Created Time: 2022-12-14
|
||||
* Author: msk397 (machangxinq@gmail.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* キー値ペア */
|
||||
struct Pair {
|
||||
public:
|
||||
int key;
|
||||
string val;
|
||||
Pair(int key, string val) {
|
||||
this->key = key;
|
||||
this->val = val;
|
||||
}
|
||||
};
|
||||
|
||||
/* 配列実装に基づくハッシュテーブル */
|
||||
class ArrayHashMap {
|
||||
private:
|
||||
vector<Pair *> buckets;
|
||||
|
||||
public:
|
||||
ArrayHashMap() {
|
||||
// 配列を初期化、100個のバケットを含む
|
||||
buckets = vector<Pair *>(100);
|
||||
}
|
||||
|
||||
~ArrayHashMap() {
|
||||
// メモリを解放
|
||||
for (const auto &bucket : buckets) {
|
||||
delete bucket;
|
||||
}
|
||||
buckets.clear();
|
||||
}
|
||||
|
||||
/* ハッシュ関数 */
|
||||
int hashFunc(int key) {
|
||||
int index = key % 100;
|
||||
return index;
|
||||
}
|
||||
|
||||
/* クエリ操作 */
|
||||
string get(int key) {
|
||||
int index = hashFunc(key);
|
||||
Pair *pair = buckets[index];
|
||||
if (pair == nullptr)
|
||||
return "";
|
||||
return pair->val;
|
||||
}
|
||||
|
||||
/* 追加操作 */
|
||||
void put(int key, string val) {
|
||||
Pair *pair = new Pair(key, val);
|
||||
int index = hashFunc(key);
|
||||
buckets[index] = pair;
|
||||
}
|
||||
|
||||
/* 削除操作 */
|
||||
void remove(int key) {
|
||||
int index = hashFunc(key);
|
||||
// メモリを解放してnullptrに設定
|
||||
delete buckets[index];
|
||||
buckets[index] = nullptr;
|
||||
}
|
||||
|
||||
/* すべてのキー値ペアを取得 */
|
||||
vector<Pair *> pairSet() {
|
||||
vector<Pair *> pairSet;
|
||||
for (Pair *pair : buckets) {
|
||||
if (pair != nullptr) {
|
||||
pairSet.push_back(pair);
|
||||
}
|
||||
}
|
||||
return pairSet;
|
||||
}
|
||||
|
||||
/* すべてのキーを取得 */
|
||||
vector<int> keySet() {
|
||||
vector<int> keySet;
|
||||
for (Pair *pair : buckets) {
|
||||
if (pair != nullptr) {
|
||||
keySet.push_back(pair->key);
|
||||
}
|
||||
}
|
||||
return keySet;
|
||||
}
|
||||
|
||||
/* すべての値を取得 */
|
||||
vector<string> valueSet() {
|
||||
vector<string> valueSet;
|
||||
for (Pair *pair : buckets) {
|
||||
if (pair != nullptr) {
|
||||
valueSet.push_back(pair->val);
|
||||
}
|
||||
}
|
||||
return valueSet;
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを印刷 */
|
||||
void print() {
|
||||
for (Pair *kv : pairSet()) {
|
||||
cout << kv->key << " -> " << kv->val << endl;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// テストケースはarray_hash_map_test.cppを参照
|
||||
52
ja/codes/cpp/chapter_hashing/array_hash_map_test.cpp
Normal file
52
ja/codes/cpp/chapter_hashing/array_hash_map_test.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* File: array_hash_map_test.cpp
|
||||
* Created Time: 2022-12-14
|
||||
* Author: msk397 (machangxinq@gmail.com)
|
||||
*/
|
||||
|
||||
#include "./array_hash_map.cpp"
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
/* ハッシュテーブルを初期化 */
|
||||
ArrayHashMap map = ArrayHashMap();
|
||||
|
||||
/* 追加操作 */
|
||||
// キー値ペア(key, value)をハッシュテーブルに追加
|
||||
map.put(12836, "Ha");
|
||||
map.put(15937, "Luo");
|
||||
map.put(16750, "Suan");
|
||||
map.put(13276, "Fa");
|
||||
map.put(10583, "Ya");
|
||||
cout << "\nAfter adding, the hash table is\nKey -> Value" << endl;
|
||||
map.print();
|
||||
|
||||
/* クエリ操作 */
|
||||
// ハッシュテーブルにキーを入力、値を取得
|
||||
string name = map.get(15937);
|
||||
cout << "\nEnter student ID 15937, found name " << name << endl;
|
||||
|
||||
/* 削除操作 */
|
||||
// ハッシュテーブルからキー値ペア(key, value)を削除
|
||||
map.remove(10583);
|
||||
cout << "\nAfter removing 10583, the hash table is\nKey -> Value" << endl;
|
||||
map.print();
|
||||
|
||||
/* ハッシュテーブルを走査 */
|
||||
cout << "\nTraverse key-value pairs Key->Value" << endl;
|
||||
for (auto kv : map.pairSet()) {
|
||||
cout << kv->key << " -> " << kv->val << endl;
|
||||
}
|
||||
|
||||
cout << "\nIndividually traverse keys Key" << endl;
|
||||
for (auto key : map.keySet()) {
|
||||
cout << key << endl;
|
||||
}
|
||||
|
||||
cout << "\nIndividually traverse values Value" << endl;
|
||||
for (auto val : map.valueSet()) {
|
||||
cout << val << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
29
ja/codes/cpp/chapter_hashing/built_in_hash.cpp
Normal file
29
ja/codes/cpp/chapter_hashing/built_in_hash.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* File: built_in_hash.cpp
|
||||
* Created Time: 2023-06-21
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
int num = 3;
|
||||
size_t hashNum = hash<int>()(num);
|
||||
cout << "The hash value of integer " << num << " is " << hashNum << "\n";
|
||||
|
||||
bool bol = true;
|
||||
size_t hashBol = hash<bool>()(bol);
|
||||
cout << "The hash value of boolean " << bol << " is " << hashBol << "\n";
|
||||
|
||||
double dec = 3.14159;
|
||||
size_t hashDec = hash<double>()(dec);
|
||||
cout << "The hash value of decimal " << dec << " is " << hashDec << "\n";
|
||||
|
||||
string str = "Hello algorithm";
|
||||
size_t hashStr = hash<string>()(str);
|
||||
cout << "The hash value of string " << str << " is " << hashStr << "\n";
|
||||
|
||||
// C++では、組み込みのstd:hash()は基本データ型のハッシュ値のみを提供
|
||||
// 配列やオブジェクトのハッシュ値計算は手動で実装する必要がある
|
||||
}
|
||||
46
ja/codes/cpp/chapter_hashing/hash_map.cpp
Normal file
46
ja/codes/cpp/chapter_hashing/hash_map.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* File: hash_map.cpp
|
||||
* Created Time: 2022-12-14
|
||||
* Author: msk397 (machangxinq@gmail.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
/* ハッシュテーブルを初期化 */
|
||||
unordered_map<int, string> map;
|
||||
|
||||
/* 追加操作 */
|
||||
// キー値ペア(key, value)をハッシュテーブルに追加
|
||||
map[12836] = "Ha";
|
||||
map[15937] = "Luo";
|
||||
map[16750] = "Suan";
|
||||
map[13276] = "Fa";
|
||||
map[10583] = "Ya";
|
||||
cout << "\nAfter adding, the hash table is\nKey -> Value" << endl;
|
||||
printHashMap(map);
|
||||
|
||||
/* クエリ操作 */
|
||||
// ハッシュテーブルにキーを入力、値を取得
|
||||
string name = map[15937];
|
||||
cout << "\nEnter student ID 15937, found name " << name << endl;
|
||||
|
||||
/* 削除操作 */
|
||||
// ハッシュテーブルからキー値ペア(key, value)を削除
|
||||
map.erase(10583);
|
||||
cout << "\nAfter removing 10583, the hash table is\nKey -> Value" << endl;
|
||||
printHashMap(map);
|
||||
|
||||
/* ハッシュテーブルを走査 */
|
||||
cout << "\nTraverse key-value pairs Key->Value" << endl;
|
||||
for (auto kv : map) {
|
||||
cout << kv.first << " -> " << kv.second << endl;
|
||||
}
|
||||
cout << "\nIterate through Key->Value using an iterator" << endl;
|
||||
for (auto iter = map.begin(); iter != map.end(); iter++) {
|
||||
cout << iter->first << "->" << iter->second << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
150
ja/codes/cpp/chapter_hashing/hash_map_chaining.cpp
Normal file
150
ja/codes/cpp/chapter_hashing/hash_map_chaining.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
/**
|
||||
* File: hash_map_chaining.cpp
|
||||
* Created Time: 2023-06-13
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "./array_hash_map.cpp"
|
||||
|
||||
/* チェイン法ハッシュテーブル */
|
||||
class HashMapChaining {
|
||||
private:
|
||||
int size; // キー値ペアの数
|
||||
int capacity; // ハッシュテーブルの容量
|
||||
double loadThres; // 拡張をトリガーする負荷率の閾値
|
||||
int extendRatio; // 拡張倍率
|
||||
vector<vector<Pair *>> buckets; // バケット配列
|
||||
|
||||
public:
|
||||
/* コンストラクタ */
|
||||
HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3.0), extendRatio(2) {
|
||||
buckets.resize(capacity);
|
||||
}
|
||||
|
||||
/* デストラクタ */
|
||||
~HashMapChaining() {
|
||||
for (auto &bucket : buckets) {
|
||||
for (Pair *pair : bucket) {
|
||||
// メモリを解放
|
||||
delete pair;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ハッシュ関数 */
|
||||
int hashFunc(int key) {
|
||||
return key % capacity;
|
||||
}
|
||||
|
||||
/* 負荷率 */
|
||||
double loadFactor() {
|
||||
return (double)size / (double)capacity;
|
||||
}
|
||||
|
||||
/* クエリ操作 */
|
||||
string get(int key) {
|
||||
int index = hashFunc(key);
|
||||
// バケットを走査、キーが見つかった場合、対応するvalを返却
|
||||
for (Pair *pair : buckets[index]) {
|
||||
if (pair->key == key) {
|
||||
return pair->val;
|
||||
}
|
||||
}
|
||||
// キーが見つからない場合、空文字列を返却
|
||||
return "";
|
||||
}
|
||||
|
||||
/* 追加操作 */
|
||||
void put(int key, string val) {
|
||||
// 負荷率が閾値を超えた場合、拡張を実行
|
||||
if (loadFactor() > loadThres) {
|
||||
extend();
|
||||
}
|
||||
int index = hashFunc(key);
|
||||
// バケットを走査、指定キーに遭遇した場合、対応するvalを更新して返却
|
||||
for (Pair *pair : buckets[index]) {
|
||||
if (pair->key == key) {
|
||||
pair->val = val;
|
||||
return;
|
||||
}
|
||||
}
|
||||
// キーが見つからない場合、キー値ペアを末尾に追加
|
||||
buckets[index].push_back(new Pair(key, val));
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 削除操作 */
|
||||
void remove(int key) {
|
||||
int index = hashFunc(key);
|
||||
auto &bucket = buckets[index];
|
||||
// バケットを走査、キー値ペアを削除
|
||||
for (int i = 0; i < bucket.size(); i++) {
|
||||
if (bucket[i]->key == key) {
|
||||
Pair *tmp = bucket[i];
|
||||
bucket.erase(bucket.begin() + i); // キー値ペアを削除
|
||||
delete tmp; // メモリを解放
|
||||
size--;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを拡張 */
|
||||
void extend() {
|
||||
// 元のハッシュテーブルを一時保存
|
||||
vector<vector<Pair *>> bucketsTmp = buckets;
|
||||
// 拡張された新しいハッシュテーブルを初期化
|
||||
capacity *= extendRatio;
|
||||
buckets.clear();
|
||||
buckets.resize(capacity);
|
||||
size = 0;
|
||||
// 元のハッシュテーブルから新しいハッシュテーブルにキー値ペアを移動
|
||||
for (auto &bucket : bucketsTmp) {
|
||||
for (Pair *pair : bucket) {
|
||||
put(pair->key, pair->val);
|
||||
// メモリを解放
|
||||
delete pair;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを印刷 */
|
||||
void print() {
|
||||
for (auto &bucket : buckets) {
|
||||
cout << "[";
|
||||
for (Pair *pair : bucket) {
|
||||
cout << pair->key << " -> " << pair->val << ", ";
|
||||
}
|
||||
cout << "]\n";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
/* ハッシュテーブルを初期化 */
|
||||
HashMapChaining map = HashMapChaining();
|
||||
|
||||
/* 追加操作 */
|
||||
// キー値ペア(key, value)をハッシュテーブルに追加
|
||||
map.put(12836, "Ha");
|
||||
map.put(15937, "Luo");
|
||||
map.put(16750, "Suan");
|
||||
map.put(13276, "Fa");
|
||||
map.put(10583, "Ya");
|
||||
cout << "\nAfter adding, the hash table is\nKey -> Value" << endl;
|
||||
map.print();
|
||||
|
||||
/* クエリ操作 */
|
||||
// ハッシュテーブルにキーを入力、値を取得
|
||||
string name = map.get(13276);
|
||||
cout << "\nEnter student ID 13276, found name " << name << endl;
|
||||
|
||||
/* 削除操作 */
|
||||
// ハッシュテーブルからキー値ペア(key, value)を削除
|
||||
map.remove(12836);
|
||||
cout << "\nAfter removing 12836, the hash table is\nKey -> Value" << endl;
|
||||
map.print();
|
||||
|
||||
return 0;
|
||||
}
|
||||
171
ja/codes/cpp/chapter_hashing/hash_map_open_addressing.cpp
Normal file
171
ja/codes/cpp/chapter_hashing/hash_map_open_addressing.cpp
Normal file
@@ -0,0 +1,171 @@
|
||||
/**
|
||||
* File: hash_map_open_addressing.cpp
|
||||
* Created Time: 2023-06-13
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "./array_hash_map.cpp"
|
||||
|
||||
/* オープンアドレス法ハッシュテーブル */
|
||||
class HashMapOpenAddressing {
|
||||
private:
|
||||
int size; // キー値ペアの数
|
||||
int capacity = 4; // ハッシュテーブルの容量
|
||||
const double loadThres = 2.0 / 3.0; // 拡張をトリガーする負荷率の閾値
|
||||
const int extendRatio = 2; // 拡張倍率
|
||||
vector<Pair *> buckets; // バケット配列
|
||||
Pair *TOMBSTONE = new Pair(-1, "-1"); // 削除マーク
|
||||
|
||||
public:
|
||||
/* コンストラクタ */
|
||||
HashMapOpenAddressing() : size(0), buckets(capacity, nullptr) {
|
||||
}
|
||||
|
||||
/* デストラクタ */
|
||||
~HashMapOpenAddressing() {
|
||||
for (Pair *pair : buckets) {
|
||||
if (pair != nullptr && pair != TOMBSTONE) {
|
||||
delete pair;
|
||||
}
|
||||
}
|
||||
delete TOMBSTONE;
|
||||
}
|
||||
|
||||
/* ハッシュ関数 */
|
||||
int hashFunc(int key) {
|
||||
return key % capacity;
|
||||
}
|
||||
|
||||
/* 負荷率 */
|
||||
double loadFactor() {
|
||||
return (double)size / capacity;
|
||||
}
|
||||
|
||||
/* keyに対応するバケットインデックスを検索 */
|
||||
int findBucket(int key) {
|
||||
int index = hashFunc(key);
|
||||
int firstTombstone = -1;
|
||||
// 線形探査、空のバケットに遭遇したら中断
|
||||
while (buckets[index] != nullptr) {
|
||||
// keyに遭遇した場合、対応するバケットインデックスを返却
|
||||
if (buckets[index]->key == key) {
|
||||
// 以前に削除マークに遭遇していた場合、キー値ペアをそのインデックスに移動
|
||||
if (firstTombstone != -1) {
|
||||
buckets[firstTombstone] = buckets[index];
|
||||
buckets[index] = TOMBSTONE;
|
||||
return firstTombstone; // 移動されたバケットインデックスを返却
|
||||
}
|
||||
return index; // バケットインデックスを返却
|
||||
}
|
||||
// 最初に遭遇した削除マークを記録
|
||||
if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {
|
||||
firstTombstone = index;
|
||||
}
|
||||
// バケットインデックスを計算、末尾を超えた場合は先頭に戻る
|
||||
index = (index + 1) % capacity;
|
||||
}
|
||||
// keyが存在しない場合、挿入ポイントのインデックスを返却
|
||||
return firstTombstone == -1 ? index : firstTombstone;
|
||||
}
|
||||
|
||||
/* クエリ操作 */
|
||||
string get(int key) {
|
||||
// keyに対応するバケットインデックスを検索
|
||||
int index = findBucket(key);
|
||||
// キー値ペアが見つかった場合、対応するvalを返却
|
||||
if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {
|
||||
return buckets[index]->val;
|
||||
}
|
||||
// キー値ペアが存在しない場合、空文字列を返却
|
||||
return "";
|
||||
}
|
||||
|
||||
/* 追加操作 */
|
||||
void put(int key, string val) {
|
||||
// 負荷率が閾値を超えた場合、拡張を実行
|
||||
if (loadFactor() > loadThres) {
|
||||
extend();
|
||||
}
|
||||
// keyに対応するバケットインデックスを検索
|
||||
int index = findBucket(key);
|
||||
// キー値ペアが見つかった場合、valを上書きして返却
|
||||
if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {
|
||||
buckets[index]->val = val;
|
||||
return;
|
||||
}
|
||||
// キー値ペアが存在しない場合、キー値ペアを追加
|
||||
buckets[index] = new Pair(key, val);
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 削除操作 */
|
||||
void remove(int key) {
|
||||
// keyに対応するバケットインデックスを検索
|
||||
int index = findBucket(key);
|
||||
// キー値ペアが見つかった場合、削除マークで覆う
|
||||
if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {
|
||||
delete buckets[index];
|
||||
buckets[index] = TOMBSTONE;
|
||||
size--;
|
||||
}
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを拡張 */
|
||||
void extend() {
|
||||
// 元のハッシュテーブルを一時保存
|
||||
vector<Pair *> bucketsTmp = buckets;
|
||||
// 拡張された新しいハッシュテーブルを初期化
|
||||
capacity *= extendRatio;
|
||||
buckets = vector<Pair *>(capacity, nullptr);
|
||||
size = 0;
|
||||
// 元のハッシュテーブルから新しいハッシュテーブルにキー値ペアを移動
|
||||
for (Pair *pair : bucketsTmp) {
|
||||
if (pair != nullptr && pair != TOMBSTONE) {
|
||||
put(pair->key, pair->val);
|
||||
delete pair;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを印刷 */
|
||||
void print() {
|
||||
for (Pair *pair : buckets) {
|
||||
if (pair == nullptr) {
|
||||
cout << "nullptr" << endl;
|
||||
} else if (pair == TOMBSTONE) {
|
||||
cout << "TOMBSTONE" << endl;
|
||||
} else {
|
||||
cout << pair->key << " -> " << pair->val << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
// ハッシュテーブルを初期化
|
||||
HashMapOpenAddressing hashmap;
|
||||
|
||||
// 追加操作
|
||||
// キー値ペア(key, val)をハッシュテーブルに追加
|
||||
hashmap.put(12836, "Ha");
|
||||
hashmap.put(15937, "Luo");
|
||||
hashmap.put(16750, "Suan");
|
||||
hashmap.put(13276, "Fa");
|
||||
hashmap.put(10583, "Ya");
|
||||
cout << "\nAfter adding, the hash table is\nKey -> Value" << endl;
|
||||
hashmap.print();
|
||||
|
||||
// クエリ操作
|
||||
// ハッシュテーブルにキーを入力、値valを取得
|
||||
string name = hashmap.get(13276);
|
||||
cout << "\nEnter student ID 13276, found name " << name << endl;
|
||||
|
||||
// 削除操作
|
||||
// ハッシュテーブルからキー値ペア(key, val)を削除
|
||||
hashmap.remove(16750);
|
||||
cout << "\nAfter removing 16750, the hash table is\nKey -> Value" << endl;
|
||||
hashmap.print();
|
||||
|
||||
return 0;
|
||||
}
|
||||
66
ja/codes/cpp/chapter_hashing/simple_hash.cpp
Normal file
66
ja/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;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user