日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

B00013 字符串哈希函数

發布時間:2023/11/29 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 B00013 字符串哈希函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

哈希算法將任意長度的二進制值映射為較短的固定長度的二進制值,這個小的二進制值稱為哈希值。

哈希函數用途廣泛,這個程序給出了絕大多數常用的哈希函數。源程序來自:哈希算法_百度百科。

程序員可以根據自己的需要取用這些代碼。

需要注意的是,有可能string類的方法已經發生變化。實際使用以下程序的時候,寫成str.charAt(i)編譯通過不了,改為str.at(i)則編譯通過。

另外,這個代碼并不是優化的代碼,對字符串處理沒有使用字符指針,處理效率低。

源程序如下:

class GeneralHashFunctionLibrary {/*RSHash*/public long RSHash(String str){int b = 378551;int a = 63689;long hash = 0;for(int i = 0; i < str.length(); i++){hash = hash * a + str.charAt(i);a = a * b;}return hash;}/*JSHash*/public long JSHash(String str){long hash = 1315423911;for(int i = 0; i < str.length(); i++)hash ^= ((hash << 5) + str.charAt(i) + (hash >> 2));return hash;}/*PJWHash*/public long PJWHash(String str){long BitsInUnsignedInt = (long)(4 * 8);long ThreeQuarters = (long)((BitsInUnsignedInt * 3) / 4);long OneEighth = (long)(BitsInUnsignedInt / 8);long HighBits = (long)(0xFFFFFFFF)<<(BitsInUnsignedInt-OneEighth);long hash = 0;long test = 0;for(int i = 0; i < str.length(); i++){hash = (hash << OneEighth) + str.charAt(i);if((test = hash & HighBits) != 0)hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));}return hash;}/*ELFHash*/public long ELFHash(String str){long hash = 0;long x = 0;for(int i = 0; i < str.length(); i++){hash = (hash << 4) + str.charAt(i);if(( x = hash & 0xF0000000L) != 0)hash ^= ( x >> 24);hash &= ~x;}return hash;}/*BKDRHash*/public long BKDRHash(String str){long seed = 131;//31131131313131131313etc..long hash = 0;for(int i = 0; i < str.length(); i++)hash = (hash * seed) + str.charAt(i);return hash;}/*SDBMHash*/public long SDBMHash(String str){long hash = 0;for(int i = 0; i < str.length(); i++)hash = str.charAt(i) + (hash << 6) + (hash << 16) - hash;return hash;}/*DJBHash*/public long DJBHash(String str){long hash = 5381;for(int i = 0; i < str.length(); i++)hash = ((hash << 5) + hash) + str.charAt(i);return hash;}/*DEKHash*/public long DEKHash(String str){long hash = str.length();for(int i = 0; i < str.length(); i++)hash = ((hash << 5) ^ (hash >> 27)) ^ str.charAt(i);return hash;}/*BPHash*/public long BPHash(String str){long hash=0;for(int i = 0;i < str.length(); i++)hash = hash << 7 ^ str.charAt(i);return hash;}/*FNVHash*/public long FNVHash(String str){long fnv_prime = 0x811C9DC5;long hash = 0;for(int i = 0; i < str.length(); i++) {hash *= fnv_prime;hash ^= str.charAt(i);}return hash;}/*APHash*/long APHash(String str){long hash = 0xAAAAAAAA;for(int i = 0; i < str.length(); i++){if((i & 1) == 0)hash ^=((hash << 7) ^ str.charAt(i) ^ (hash >> 3));elsehash ^= (~((hash << 11) ^ str.charAt(i) ^ (hash >> 5)));}return hash;} }

轉載于:https://www.cnblogs.com/tigerisland/p/7564728.html

總結

以上是生活随笔為你收集整理的B00013 字符串哈希函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。