C/C++/C#/JAVA/PHP/JAVASCRIPT/PYTHON 中文字串雜湊值(雜湊函數/雜湊表)

C/C++/C#/JAVA/PHP/JAVASCRIPT/PYTHON 中文字串雜湊值(雜湊函數/雜湊表)

C/C++/C#/JAVA/PHP/JAVASCRIPT/PYTHON 中文字串雜湊值(雜湊函數/雜湊表)~二分搜尋法前置動作


資料來源: Copilot


C# Code

using System;

class Program
{
    static int Hash(string str)
    {
        int hash = 0;
        foreach (char c in str)
        {
            hash = (hash << 5) + hash + c;
        }
        return hash;
    }

    static void Main()
    {
        string str = "你好,世界";
        Console.WriteLine("Hash value: " + Hash(str));
    }
}

純C Code

/* Online C Compiler and Editor */
#include <stdio.h>

unsigned int hash(const char *str) {
    unsigned int hash = 0;
    while (*str) {
        hash = (hash << 5) + hash + (unsigned char)(*str);
        str++;
    }
    return hash;
}

int main() {
    const char *str = "你好,世界";
    printf("Hash value: %u\n", hash(str));

    printf("Hash value: %u\n", hash("jash.liao"));     
    return 0;
}

C++ Code

#include <iostream>
#include <string>

unsigned int hash(const std::string &str) {
    unsigned int hash = 0;
    for (char c : str) {
        hash = (hash << 5) + hash + static_cast<unsigned char>(c);
    }
    return hash;
}

int main() {
    std::string str = "你好,世界";
    std::cout << "Hash value: " << hash(str) << std::endl;
    return 0;
}

JAVA Code

public class HashExample {
    public static int hash(String str) {
        int hash = 0;
        for (char c : str.toCharArray()) {
            hash = (hash << 5) + hash + (int) c;
        }
        return hash;
    }

    public static void main(String[] args) {
        String str = "你好,世界";
        System.out.println("Hash value: " + hash(str));
    }
}

PHP Code

<?php
function hashString($str) {
    $hash = 0;
    $length = strlen($str);
    for ($i = 0; $i < $length; $i++) {
        $hash = ($hash << 5) + $hash + ord($str[$i]);
    }
    return $hash;
}

$str = "你好,世界";
echo "Hash value: " . hashString($str) . "\n";
?>

JAVASCRIPT Code

function hashString(str) {
    let hash = 0;
    for (let i = 0; i < str.length; i++) {
        hash = (hash << 5) + hash + str.charCodeAt(i);
    }
    return hash;
}

const str = "你好,世界";
console.log("Hash value: " + hashString(str));

PYTHON Code

def hash_string(s):
    hash = 0
    for c in s:
        hash = (hash << 5) + hash + ord(c)
    return hash

str = "你好,世界"
print("Hash value:", hash_string(str))

8 thoughts on “C/C++/C#/JAVA/PHP/JAVASCRIPT/PYTHON 中文字串雜湊值(雜湊函數/雜湊表)

  1. 目前常見的雜湊演算法

    https://zh.wikipedia.org/zh-tw/%E6%95%A3%E5%88%97%E5%87%BD%E6%95%B8

    演算法名稱 輸出大小(bits) 內部大小 區塊大小 長度大小 字元尺寸 碰撞情形
    HAVAL 256/224/192/160/128 256 1024 64 32 是
    MD2 128 384 128 No 8 大多數
    MD4 128 128 512 64 32 是
    MD5 128 128 512 64 32 是
    PANAMA 256 8736 256 否 32 是
    RadioGatún 任意長度 58字 3字 否 1-64 否
    RIPEMD 128 128 512 64 32 是
    RIPEMD-128/256 128/256 128/256 512 64 32 否
    RIPEMD-160/320 160/320 160/320 512 64 32 否
    SHA-0 160 160 512 64 32 是
    SHA-1 160 160 512 64 32 有缺陷
    SHA-256/224 256/224 256 512 64 32 否
    SHA-512/384 512/384 512 1024 128 64 否
    Tiger(2)-192/160/128 192/160/128 192 512 64 64 否
    WHIRLPOOL 512 512 512 256 8 否

  2. https://www.hello-algo.com/zh-hant/chapter_hashing/hash_algorithm/#631

    純C 的其他各種雜湊演算法

    /* 加法雜湊 */
    int addHash(char *key) {
    long long hash = 0;
    const int MODULUS = 1000000007;
    for (int i = 0; i < strlen(key); i++) {
    hash = (hash + (unsigned char)key[i]) % MODULUS;
    }
    return (int)hash;
    }

    /* 乘法雜湊 */
    int mulHash(char *key) {
    long long hash = 0;
    const int MODULUS = 1000000007;
    for (int i = 0; i < strlen(key); i++) {
    hash = (31 * hash + (unsigned char)key[i]) % MODULUS;
    }
    return (int)hash;
    }

    /* 互斥或雜湊 */
    int xorHash(char *key) {
    int hash = 0;
    const int MODULUS = 1000000007;

    for (int i = 0; i < strlen(key); i++) {
    hash ^= (unsigned char)key[i];
    }
    return hash & MODULUS;
    }

    /* 旋轉雜湊 */
    int rotHash(char *key) {
    long long hash = 0;
    const int MODULUS = 1000000007;
    for (int i = 0; i < strlen(key); i++) {
    hash = ((hash <> 28) ^ (unsigned char)key[i]) % MODULUS;
    }

    return (int)hash;
    }

  3. https://www.hello-algo.com/zh-hant/chapter_hashing/hash_algorithm/#631

    C++ 的其他各種雜湊演算法

    /* 加法雜湊 */
    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;
    }

    /* 互斥或雜湊 */
    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 <> 28) ^ (int)c) % MODULUS;
    }
    return (int)hash;
    }

  4. https://www.hello-algo.com/zh-hant/chapter_hashing/hash_algorithm/#631

    Python 的其他各種雜湊演算法

    def add_hash(key: str) -> int:
    """加法雜湊"""
    hash = 0
    modulus = 1000000007
    for c in key:
    hash += ord(c)
    return hash % modulus

    def mul_hash(key: str) -> int:
    """乘法雜湊"""
    hash = 0
    modulus = 1000000007
    for c in key:
    hash = 31 * hash + ord(c)
    return hash % modulus

    def xor_hash(key: str) -> int:
    """互斥或雜湊"""
    hash = 0
    modulus = 1000000007
    for c in key:
    hash ^= ord(c)
    return hash % modulus

    def rot_hash(key: str) -> int:
    """旋轉雜湊"""
    hash = 0
    modulus = 1000000007
    for c in key:
    hash = (hash <> 28) ^ ord(c)
    return hash % modulus

  5. https://www.hello-algo.com/zh-hant/chapter_hashing/hash_algorithm/#631

    JAVA 的其他各種雜湊演算法

    /* 加法雜湊 */
    int addHash(String key) {
    long hash = 0;
    final int MODULUS = 1000000007;
    for (char c : key.toCharArray()) {
    hash = (hash + (int) c) % MODULUS;
    }
    return (int) hash;
    }

    /* 乘法雜湊 */
    int mulHash(String key) {
    long hash = 0;
    final int MODULUS = 1000000007;
    for (char c : key.toCharArray()) {
    hash = (31 * hash + (int) c) % MODULUS;
    }
    return (int) hash;
    }

    /* 互斥或雜湊 */
    int xorHash(String key) {
    int hash = 0;
    final int MODULUS = 1000000007;
    for (char c : key.toCharArray()) {
    hash ^= (int) c;
    }
    return hash & MODULUS;
    }

    /* 旋轉雜湊 */
    int rotHash(String key) {
    long hash = 0;
    final int MODULUS = 1000000007;
    for (char c : key.toCharArray()) {
    hash = ((hash <> 28) ^ (int) c) % MODULUS;
    }
    return (int) hash;
    }

  6. https://www.hello-algo.com/zh-hant/chapter_hashing/hash_algorithm/#631

    C# 的其他各種雜湊演算法


    /* 加法雜湊 */
    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;
    }

    /* 互斥或雜湊 */
    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 <> 28) ^ c) % MODULUS;
    }
    return (int)hash;
    }

  7. https://www.hello-algo.com/zh-hant/chapter_hashing/hash_algorithm/#631

    Javascript 的其他各種雜湊演算法


    /* 加法雜湊 */
    function addHash(key) {
    let hash = 0;
    const MODULUS = 1000000007;
    for (const c of key) {
    hash = (hash + c.charCodeAt(0)) % MODULUS;
    }
    return hash;
    }

    /* 乘法雜湊 */
    function mulHash(key) {
    let hash = 0;
    const MODULUS = 1000000007;
    for (const c of key) {
    hash = (31 * hash + c.charCodeAt(0)) % MODULUS;
    }
    return hash;
    }

    /* 互斥或雜湊 */
    function xorHash(key) {
    let hash = 0;
    const MODULUS = 1000000007;
    for (const c of key) {
    hash ^= c.charCodeAt(0);
    }
    return hash & MODULUS;
    }

    /* 旋轉雜湊 */
    function rotHash(key) {
    let hash = 0;
    const MODULUS = 1000000007;
    for (const c of key) {
    hash = ((hash <> 28) ^ c.charCodeAt(0)) % MODULUS;
    }
    return hash;
    }

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *