mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-10 22:46:07 +00:00
Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4 * Retranslate Japanese code with GPT-5.4
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
/**
|
||||
* File: array_hash_map.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_hashing;
|
||||
|
||||
/* キーと値の組 int->string */
|
||||
class Pair(int key, string val) {
|
||||
public int key = key;
|
||||
public string val = val;
|
||||
}
|
||||
|
||||
/* 配列ベースのハッシュテーブル */
|
||||
class ArrayHashMap {
|
||||
List<Pair?> buckets;
|
||||
public ArrayHashMap() {
|
||||
// 100 個のバケットを含む配列を初期化
|
||||
buckets = [];
|
||||
for (int i = 0; i < 100; i++) {
|
||||
buckets.Add(null);
|
||||
}
|
||||
}
|
||||
|
||||
/* ハッシュ関数 */
|
||||
int HashFunc(int key) {
|
||||
int index = key % 100;
|
||||
return index;
|
||||
}
|
||||
|
||||
/* 検索操作 */
|
||||
public string? Get(int key) {
|
||||
int index = HashFunc(key);
|
||||
Pair? pair = buckets[index];
|
||||
if (pair == null) return null;
|
||||
return pair.val;
|
||||
}
|
||||
|
||||
/* 追加操作 */
|
||||
public void Put(int key, string val) {
|
||||
Pair pair = new(key, val);
|
||||
int index = HashFunc(key);
|
||||
buckets[index] = pair;
|
||||
}
|
||||
|
||||
/* 削除操作 */
|
||||
public void Remove(int key) {
|
||||
int index = HashFunc(key);
|
||||
// null に設定し、削除を表す
|
||||
buckets[index] = null;
|
||||
}
|
||||
|
||||
/* すべてのキーと値のペアを取得 */
|
||||
public List<Pair> PairSet() {
|
||||
List<Pair> pairSet = [];
|
||||
foreach (Pair? pair in buckets) {
|
||||
if (pair != null)
|
||||
pairSet.Add(pair);
|
||||
}
|
||||
return pairSet;
|
||||
}
|
||||
|
||||
/* すべてのキーを取得 */
|
||||
public List<int> KeySet() {
|
||||
List<int> keySet = [];
|
||||
foreach (Pair? pair in buckets) {
|
||||
if (pair != null)
|
||||
keySet.Add(pair.key);
|
||||
}
|
||||
return keySet;
|
||||
}
|
||||
|
||||
/* すべての値を取得 */
|
||||
public List<string> ValueSet() {
|
||||
List<string> valueSet = [];
|
||||
foreach (Pair? pair in buckets) {
|
||||
if (pair != null)
|
||||
valueSet.Add(pair.val);
|
||||
}
|
||||
return valueSet;
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを出力 */
|
||||
public void Print() {
|
||||
foreach (Pair kv in PairSet()) {
|
||||
Console.WriteLine(kv.key + " -> " + kv.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class array_hash_map {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* ハッシュテーブルを初期化 */
|
||||
ArrayHashMap map = new();
|
||||
|
||||
/* 追加操作 */
|
||||
// ハッシュテーブルにキーと値のペア (key, value) を追加
|
||||
map.Put(12836, "シャオハー");
|
||||
map.Put(15937, "シャオルオ");
|
||||
map.Put(16750, "シャオスワン");
|
||||
map.Put(13276, "シャオファー");
|
||||
map.Put(10583, "シャオヤー");
|
||||
Console.WriteLine("\n追加完了後、ハッシュテーブルは\nKey -> Value");
|
||||
map.Print();
|
||||
|
||||
/* 検索操作 */
|
||||
// キー key をハッシュテーブルに渡し、値 value を取得
|
||||
string? name = map.Get(15937);
|
||||
Console.WriteLine("\n学籍番号 15937 を入力すると、氏名 " + name);
|
||||
|
||||
/* 削除操作 */
|
||||
// ハッシュテーブルからキーと値のペア (key, value) を削除
|
||||
map.Remove(10583);
|
||||
Console.WriteLine("\n10583 を削除した後、ハッシュテーブルは\nKey -> Value");
|
||||
map.Print();
|
||||
|
||||
/* ハッシュテーブルを走査 */
|
||||
Console.WriteLine("\nキーと値のペア Key->Value を走査");
|
||||
foreach (Pair kv in map.PairSet()) {
|
||||
Console.WriteLine(kv.key + " -> " + kv.val);
|
||||
}
|
||||
Console.WriteLine("\nキー Key のみを走査");
|
||||
foreach (int key in map.KeySet()) {
|
||||
Console.WriteLine(key);
|
||||
}
|
||||
Console.WriteLine("\n値 Value のみを走査");
|
||||
foreach (string val in map.ValueSet()) {
|
||||
Console.WriteLine(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* File: built_in_hash.cs
|
||||
* Created Time: 2023-06-26
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_hashing;
|
||||
|
||||
public class built_in_hash {
|
||||
[Test]
|
||||
public void Test() {
|
||||
int num = 3;
|
||||
int hashNum = num.GetHashCode();
|
||||
Console.WriteLine("整数 " + num + " のハッシュ値は " + hashNum);
|
||||
|
||||
bool bol = true;
|
||||
int hashBol = bol.GetHashCode();
|
||||
Console.WriteLine("真偽値 " + bol + " のハッシュ値は " + hashBol);
|
||||
|
||||
double dec = 3.14159;
|
||||
int hashDec = dec.GetHashCode();
|
||||
Console.WriteLine("小数 " + dec + " のハッシュ値は " + hashDec);
|
||||
|
||||
string str = "Hello アルゴリズム";
|
||||
int hashStr = str.GetHashCode();
|
||||
Console.WriteLine("文字列 " + str + " のハッシュ値は " + hashStr);
|
||||
|
||||
object[] arr = [12836, "シャオハー"];
|
||||
int hashTup = arr.GetHashCode();
|
||||
Console.WriteLine("配列 [" + string.Join(", ", arr) + "] のハッシュ値は " + hashTup);
|
||||
|
||||
ListNode obj = new(0);
|
||||
int hashObj = obj.GetHashCode();
|
||||
Console.WriteLine("ノードオブジェクト " + obj + " のハッシュ値は " + hashObj);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
|
||||
/**
|
||||
* File: hash_map.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_hashing;
|
||||
|
||||
public class hash_map {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* ハッシュテーブルを初期化 */
|
||||
Dictionary<int, string> map = new() {
|
||||
/* 追加操作 */
|
||||
// ハッシュテーブルにキーと値のペア (key, value) を追加
|
||||
{ 12836, "シャオハー" },
|
||||
{ 15937, "シャオルオ" },
|
||||
{ 16750, "シャオスワン" },
|
||||
{ 13276, "シャオファー" },
|
||||
{ 10583, "シャオヤー" }
|
||||
};
|
||||
Console.WriteLine("\n追加完了後、ハッシュテーブルは\nKey -> Value");
|
||||
PrintUtil.PrintHashMap(map);
|
||||
|
||||
/* 検索操作 */
|
||||
// キー key をハッシュテーブルに渡し、値 value を取得
|
||||
string name = map[15937];
|
||||
Console.WriteLine("\n学籍番号 15937 を入力すると、氏名 " + name);
|
||||
|
||||
/* 削除操作 */
|
||||
// ハッシュテーブルからキーと値のペア (key, value) を削除
|
||||
map.Remove(10583);
|
||||
Console.WriteLine("\n10583 を削除した後、ハッシュテーブルは\nKey -> Value");
|
||||
PrintUtil.PrintHashMap(map);
|
||||
|
||||
/* ハッシュテーブルを走査 */
|
||||
Console.WriteLine("\nキーと値のペア Key->Value を走査");
|
||||
foreach (var kv in map) {
|
||||
Console.WriteLine(kv.Key + " -> " + kv.Value);
|
||||
}
|
||||
Console.WriteLine("\nキー Key のみを走査");
|
||||
foreach (int key in map.Keys) {
|
||||
Console.WriteLine(key);
|
||||
}
|
||||
Console.WriteLine("\n値 Value のみを走査");
|
||||
foreach (string val in map.Values) {
|
||||
Console.WriteLine(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* File: hash_map_chaining.cs
|
||||
* Created Time: 2023-06-26
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_hashing;
|
||||
|
||||
/* チェイン法ハッシュテーブル */
|
||||
class HashMapChaining {
|
||||
int size; // キーと値のペア数
|
||||
int capacity; // ハッシュテーブル容量
|
||||
double loadThres; // リサイズを発動する負荷率のしきい値
|
||||
int extendRatio; // 拡張倍率
|
||||
List<List<Pair>> buckets; // バケット配列
|
||||
|
||||
/* コンストラクタ */
|
||||
public HashMapChaining() {
|
||||
size = 0;
|
||||
capacity = 4;
|
||||
loadThres = 2.0 / 3.0;
|
||||
extendRatio = 2;
|
||||
buckets = new List<List<Pair>>(capacity);
|
||||
for (int i = 0; i < capacity; i++) {
|
||||
buckets.Add([]);
|
||||
}
|
||||
}
|
||||
|
||||
/* ハッシュ関数 */
|
||||
int HashFunc(int key) {
|
||||
return key % capacity;
|
||||
}
|
||||
|
||||
/* 負荷率 */
|
||||
double LoadFactor() {
|
||||
return (double)size / capacity;
|
||||
}
|
||||
|
||||
/* 検索操作 */
|
||||
public string? Get(int key) {
|
||||
int index = HashFunc(key);
|
||||
// バケットを走査し、key が見つかれば対応する val を返す
|
||||
foreach (Pair pair in buckets[index]) {
|
||||
if (pair.key == key) {
|
||||
return pair.val;
|
||||
}
|
||||
}
|
||||
// key が見つからない場合は null を返す
|
||||
return null;
|
||||
}
|
||||
|
||||
/* 追加操作 */
|
||||
public void Put(int key, string val) {
|
||||
// 負荷率がしきい値を超えたら、リサイズを実行
|
||||
if (LoadFactor() > loadThres) {
|
||||
Extend();
|
||||
}
|
||||
int index = HashFunc(key);
|
||||
// バケットを走査し、指定した key が見つかれば対応する val を更新して返す
|
||||
foreach (Pair pair in buckets[index]) {
|
||||
if (pair.key == key) {
|
||||
pair.val = val;
|
||||
return;
|
||||
}
|
||||
}
|
||||
// その key が存在しなければ、キーと値のペアを末尾に追加
|
||||
buckets[index].Add(new Pair(key, val));
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 削除操作 */
|
||||
public void Remove(int key) {
|
||||
int index = HashFunc(key);
|
||||
// バケットを走査してキーと値のペアを削除
|
||||
foreach (Pair pair in buckets[index].ToList()) {
|
||||
if (pair.key == key) {
|
||||
buckets[index].Remove(pair);
|
||||
size--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを拡張 */
|
||||
void Extend() {
|
||||
// 元のハッシュテーブルを一時保存
|
||||
List<List<Pair>> bucketsTmp = buckets;
|
||||
// リサイズ後の新しいハッシュテーブルを初期化
|
||||
capacity *= extendRatio;
|
||||
buckets = new List<List<Pair>>(capacity);
|
||||
for (int i = 0; i < capacity; i++) {
|
||||
buckets.Add([]);
|
||||
}
|
||||
size = 0;
|
||||
// キーと値のペアを元のハッシュテーブルから新しいハッシュテーブルへ移す
|
||||
foreach (List<Pair> bucket in bucketsTmp) {
|
||||
foreach (Pair pair in bucket) {
|
||||
Put(pair.key, pair.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを出力 */
|
||||
public void Print() {
|
||||
foreach (List<Pair> bucket in buckets) {
|
||||
List<string> res = [];
|
||||
foreach (Pair pair in bucket) {
|
||||
res.Add(pair.key + " -> " + pair.val);
|
||||
}
|
||||
foreach (string kv in res) {
|
||||
Console.WriteLine(kv);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class hash_map_chaining {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* ハッシュテーブルを初期化 */
|
||||
HashMapChaining map = new();
|
||||
|
||||
/* 追加操作 */
|
||||
// ハッシュテーブルにキーと値のペア (key, value) を追加
|
||||
map.Put(12836, "シャオハー");
|
||||
map.Put(15937, "シャオルオ");
|
||||
map.Put(16750, "シャオスワン");
|
||||
map.Put(13276, "シャオファー");
|
||||
map.Put(10583, "シャオヤー");
|
||||
Console.WriteLine("\n追加完了後、ハッシュテーブルは\nKey -> Value");
|
||||
map.Print();
|
||||
|
||||
/* 検索操作 */
|
||||
// キー key をハッシュテーブルに渡し、値 value を取得
|
||||
string? name = map.Get(13276);
|
||||
Console.WriteLine("\n学籍番号 13276 を入力すると、氏名 " + name);
|
||||
|
||||
/* 削除操作 */
|
||||
// ハッシュテーブルからキーと値のペア (key, value) を削除
|
||||
map.Remove(12836);
|
||||
Console.WriteLine("\n12836 を削除した後、ハッシュテーブルは\nKey -> Value");
|
||||
map.Print();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
/**
|
||||
* File: hash_map_open_addressing.cs
|
||||
* Created Time: 2023-06-26
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_hashing;
|
||||
|
||||
/* オープンアドレス法ハッシュテーブル */
|
||||
class HashMapOpenAddressing {
|
||||
int size; // キーと値のペア数
|
||||
int capacity = 4; // ハッシュテーブル容量
|
||||
double loadThres = 2.0 / 3.0; // リサイズを発動する負荷率のしきい値
|
||||
int extendRatio = 2; // 拡張倍率
|
||||
Pair[] buckets; // バケット配列
|
||||
Pair TOMBSTONE = new(-1, "-1"); // 削除済みマーク
|
||||
|
||||
/* コンストラクタ */
|
||||
public HashMapOpenAddressing() {
|
||||
size = 0;
|
||||
buckets = new Pair[capacity];
|
||||
}
|
||||
|
||||
/* ハッシュ関数 */
|
||||
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] != null) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
/* 検索操作 */
|
||||
public string? Get(int key) {
|
||||
// key に対応するバケットインデックスを探す
|
||||
int index = FindBucket(key);
|
||||
// キーと値の組が見つかったら、対応する val を返す
|
||||
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
|
||||
return buckets[index].val;
|
||||
}
|
||||
// キーと値の組が存在しなければ null を返す
|
||||
return null;
|
||||
}
|
||||
|
||||
/* 追加操作 */
|
||||
public void Put(int key, string val) {
|
||||
// 負荷率がしきい値を超えたら、リサイズを実行
|
||||
if (LoadFactor() > loadThres) {
|
||||
Extend();
|
||||
}
|
||||
// key に対応するバケットインデックスを探す
|
||||
int index = FindBucket(key);
|
||||
// キーと値の組が見つかったら、val を上書きして返す
|
||||
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
|
||||
buckets[index].val = val;
|
||||
return;
|
||||
}
|
||||
// キーと値の組が存在しない場合は、その組を追加する
|
||||
buckets[index] = new Pair(key, val);
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 削除操作 */
|
||||
public void Remove(int key) {
|
||||
// key に対応するバケットインデックスを探す
|
||||
int index = FindBucket(key);
|
||||
// キーと値の組が見つかったら、削除マーカーで上書きする
|
||||
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
|
||||
buckets[index] = TOMBSTONE;
|
||||
size--;
|
||||
}
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを拡張 */
|
||||
void Extend() {
|
||||
// 元のハッシュテーブルを一時保存
|
||||
Pair[] bucketsTmp = buckets;
|
||||
// リサイズ後の新しいハッシュテーブルを初期化
|
||||
capacity *= extendRatio;
|
||||
buckets = new Pair[capacity];
|
||||
size = 0;
|
||||
// キーと値のペアを元のハッシュテーブルから新しいハッシュテーブルへ移す
|
||||
foreach (Pair pair in bucketsTmp) {
|
||||
if (pair != null && pair != TOMBSTONE) {
|
||||
Put(pair.key, pair.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを出力 */
|
||||
public void Print() {
|
||||
foreach (Pair pair in buckets) {
|
||||
if (pair == null) {
|
||||
Console.WriteLine("null");
|
||||
} else if (pair == TOMBSTONE) {
|
||||
Console.WriteLine("TOMBSTONE");
|
||||
} else {
|
||||
Console.WriteLine(pair.key + " -> " + pair.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class hash_map_open_addressing {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* ハッシュテーブルを初期化 */
|
||||
HashMapOpenAddressing map = new();
|
||||
|
||||
/* 追加操作 */
|
||||
// ハッシュテーブルにキーと値のペア (key, value) を追加
|
||||
map.Put(12836, "シャオハー");
|
||||
map.Put(15937, "シャオルオ");
|
||||
map.Put(16750, "シャオスワン");
|
||||
map.Put(13276, "シャオファー");
|
||||
map.Put(10583, "シャオヤー");
|
||||
Console.WriteLine("\n追加完了後、ハッシュテーブルは\nKey -> Value");
|
||||
map.Print();
|
||||
|
||||
/* 検索操作 */
|
||||
// キー key をハッシュテーブルに渡し、値 value を取得
|
||||
string? name = map.Get(13276);
|
||||
Console.WriteLine("\n学籍番号 13276 を入力すると、氏名 " + name);
|
||||
|
||||
/* 削除操作 */
|
||||
// ハッシュテーブルからキーと値のペア (key, value) を削除
|
||||
map.Remove(16750);
|
||||
Console.WriteLine("\n16750 を削除した後、ハッシュテーブルは\nKey -> Value");
|
||||
map.Print();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* 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 アルゴリズム";
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user