zoukankan      html  css  js  c++  java
  • 关于Hash的几种常用算法

    1  RSHash

     1 /* 【算法】RSHash(因Robert Sedgwicks在其《Algorithms in C》一书中展示而得名)
     2  * 【说明】63689和378551都是质数,之所以取这两个数,我想是因为抗碰撞小(散列分布均匀)
     3  * 【时间】祁俊辉->2017.5.17
     4  * */
     5 public class RSHash {
     6     //RSHash算法
     7     static long RS_Hash(String str){
     8         int a=63689;
     9         int b=378551;
    10         long hash=0;
    11         for(int i=0;i<str.length();i++){
    12             hash=hash*a+str.charAt(i);
    13             //System.out.println(hash);
    14             a=a*b;
    15             //System.out.println(a);
    16         }
    17         return (hash & 0x7FFFFFFF);//32位
    18         //return (hash & 0x7FFFFFFFFFFFFFFFL);//64位
    19     }
    20     //主函数
    21     public static void main(String[] args) {
    22         System.out.println(Long.toBinaryString(RS_Hash("祁俊辉")));
    23     }
    24 }

    2  BKDRHash

     1 /* 【算法】BKDRHash(Java字符串类的Hash算法,累成因子取31)
     2  * 【说明】累成因子可以为31/131/1313/13131/131313...
     3  * 【时间】祁俊辉->2017.5.17
     4  * */
     5 public class BKDRHash {
     6     //BKDRHash算法
     7     static long BKDR_Hash(String str){
     8         long seed=131;
     9         long hash=0;
    10         for(int i=0;i<str.length();i++){
    11             hash=hash*seed+str.charAt(i);
    12             //System.out.println(hash);
    13         }
    14         return (hash & 0x7FFFFFFF);//32位
    15         //return (hash & 0x7FFFFFFFFFFFFFFFL);//64位
    16     }
    17     //主函数
    18     public static void main(String[] args) {
    19         System.out.println(Long.toBinaryString(BKDR_Hash("祁俊辉")));
    20     }
    21 }

    3  DJBHash

     1 /* 【算法】DJBHash(目前公布最有效的Hash算法)
     2  * 【说明】俗称"Times33"算法
     3  * 【时间】祁俊辉->2017.5.17
     4  * */
     5 public class DJBHash {
     6     //DJBHash算法
     7     static long DJB_Hash(String str){
     8         long hash=5381;
     9         for(int i=0;i<str.length();i++){
    10             hash=((hash<<5)+hash)+str.charAt(i);
    11             //System.out.println(hash);
    12         }
    13         return (hash & 0x7FFFFFFF);//32位
    14         //return (hash & 0x7FFFFFFFFFFFFFFFL);//64位
    15     }
    16     //主函数
    17     public static void main(String[] args) {
    18         System.out.println(Long.toBinaryString(DJB_Hash("祁俊辉")));
    19     }
    20 }

    4  JSHash

     1 /* 【算法】JSHash(由Justin Sobel发明的一种hash算法)
     2  * 【说明】位操作
     3  * 【时间】祁俊辉->2017.5.18
     4  * */
     5 public class JSHash {
     6     //JSHash算法
     7     static long JS_Hash(String str){
     8         long hash=1315423911;
     9         for(int i=0;i<str.length();i++){
    10             hash ^= ((hash << 5) + str.charAt(i) + (hash >> 2));
    11             //System.out.println(hash);
    12         }
    13         return (hash & 0x7FFFFFFF);//32位
    14         //return (hash & 0x7FFFFFFFFFFFFFFFL);//64位
    15     }
    16     //主函数
    17     public static void main(String[] args) {
    18         System.out.println(Long.toBinaryString(JS_Hash("祁俊辉")));
    19     }
    20 }

    5  SDBMHash

     1 /* 【算法】SDBMHash
     2  * 【说明】与BKDRHash思想一致,只是数乘因子不同
     3  * 【时间】祁俊辉->2017.5.18
     4  * */
     5 public class SDBMHash {
     6     //SDBMHash算法
     7     static long SDBM_Hash(String str){
     8         long hash=0;
     9         for(int i=0;i<str.length();i++){
    10             hash=hash*65599+str.charAt(i);
    11             //hash=str.charAt(i)+(hash<<6)+(hash<<16)-hash;
    12             //System.out.println(hash);
    13         }
    14         return (hash & 0x7FFFFFFF);//32位
    15         //return (hash & 0x7FFFFFFFFFFFFFFFL);//64位
    16     }
    17     //主函数
    18     public static void main(String[] args) {
    19         System.out.println(Long.toBinaryString(SDBM_Hash("祁俊辉")));
    20     }
    21 }
  • 相关阅读:
    Unity3D脚本修改默认编码界面
    Winform异步初始化UserControl的问题
    Windows API实现移动窗体
    BackgroundWorder控件
    Winform复杂界面异步加载
    TabControl设置选项卡的大小
    VS2010尝试运行项目时出错,无法启动程序
    winform开发-CheckedListBox控件
    tomcat配置https访问
    用户svn密码自定义
  • 原文地址:https://www.cnblogs.com/qijunhui/p/8445484.html
Copyright © 2011-2022 走看看