mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	* Update avatar's link in the landing page * Bug fixes * Move assets folder from overrides to docs * Reduce figures' corner radius * Update copyright * Update header image * Krahets -> krahets * Update the landing page
		
			
				
	
	
		
			67 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
/**
 | 
						|
 * 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;
 | 
						|
}
 | 
						|
 | 
						|
/* 异或哈希 */
 | 
						|
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 dsad3241241dsa算123法";
 | 
						|
 | 
						|
    int hash = addHash(key);
 | 
						|
    cout << "加法哈希值为 " << hash << endl;
 | 
						|
 | 
						|
    hash = mulHash(key);
 | 
						|
    cout << "乘法哈希值为 " << hash << endl;
 | 
						|
 | 
						|
    hash = xorHash(key);
 | 
						|
    cout << "异或哈希值为 " << hash << endl;
 | 
						|
 | 
						|
    hash = rotHash(key);
 | 
						|
    cout << "旋转哈希值为 " << hash << endl;
 | 
						|
 | 
						|
    return 0;
 | 
						|
}
 |