zoukankan      html  css  js  c++  java
  • 哈希表

    模板    字符串哈希

     1 #include<stdio.h>
     2 #include<string.h>
     3 #define MOD 1223337
     4 struct Hash_map
     5 {
     6     int num[MOD+2];
     7     char s[MOD+2][12];
     8     void next_key(int &mod,int &value,int &key)
     9     {
    10         value++;
    11         key=value%mod;
    12     }
    13     int str_hash(char str[])
    14     {
    15         unsigned int hash=0;
    16         while(*str)
    17         {
    18             hash=(hash<<16)+(hash<<6)-hash+*(str++);
    19         }
    20         return hash&0x7FFFFFFF;
    21     }
    22     void insert(char str[])
    23     {
    24         int value=str_hash(str),mod=MOD,key=0;
    25         next_key(mod,value,key);
    26         while(num[key])
    27         {
    28             if(strcmp(str,s[key])==0)
    29             {
    30                 num[key]++;
    31                 return ;
    32             }
    33             next_key(mod,value,key);
    34         }
    35         strcpy(s[key],str);
    36         num[key]++;
    37     }
    38     int find(char str[])
    39     {
    40         int value=str_hash(str),mod=MOD,key=0;
    41         next_key(mod,value,key);
    42         while(num[key])
    43         {
    44 
    45             if(strcmp(str,s[key])==0)
    46             {
    47                 return num[key];
    48             }
    49             next_key(mod,value,key);
    50         }
    51         return 0;
    52     }
    53 } Hash;
  • 相关阅读:
    ES6中的基础语法
    let和const、var
    iframe框架
    ajax
    面试题
    移动端的点击延迟事件
    移动端如何设置字体
    swiper插件以及简介
    第十二章 systemctl管理脚本
    第十一章 awk命令
  • 原文地址:https://www.cnblogs.com/xseventh/p/7305275.html
Copyright © 2011-2022 走看看