zoukankan      html  css  js  c++  java
  • HDU 1800 hash 找出现最多次数的字符串的次数

    乘法hash:

    这类hash函数利用了乘法的不相关性

    int Hash(char *str)
    {
        int seed = 131 , value=0;
        while(*str != ''){
            value = value*seed+(*str++);
        }
        return value&0x7fffffff;
    }

    这里用的乘数是131 , 还推荐的乘数还有1313 , 13131 , 131313等

    除了乘以一个固定的数,常见的还有乘以一个不断改变的数,比如:

     int Hash(char *str)
    {
        int b = 378551 , a = 63689;

      int hash = 0;
        while(*str != ''){
            hash = hash*a+(*str++);

        a*a*b;
        }
        return value&0x7fffffff;
    }

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 using namespace std;
     6 char str[105];
     7 int _hash[5000];
     8 
     9 int Hash(char *str)
    10 {
    11     while(*str == '0') str++; //这道题目的字符串要去除前导0
    12     int seed = 131 , value=0;
    13     while(*str != ''){
    14         value = value*seed+(*str++);
    15     }
    16     return value;
    17 }
    18 
    19 int main()
    20 {
    21   //  freopen("a.in" , "r" , stdin);
    22     int n;
    23     while(~scanf("%d" , &n))
    24     {
    25         memset(_hash , 0 , sizeof(_hash));
    26         for(int i=0 ; i<n ; i++)
    27         {
    28             scanf("%s" , str);
    29             int index = Hash(str);
    30           //  cout<<"index: "<<i<<" "<<index<<endl;
    31             _hash[i]=index;
    32         }
    33         sort(_hash , _hash+n);
    34         int ans = 0;
    35         int cnt = 1;
    36         for(int i=1 ; i<n ; i++){
    37             if(_hash[i] == _hash[i-1]){
    38                 cnt++;
    39 
    40             }
    41             else{
    42                 ans = max(ans , cnt);
    43                 cnt = 1;
    44             }
    45         }
    46         ans = max(ans , cnt);
    47         printf("%d
    " , ans);
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    微信读书分享组队群
    骚操作!利用百度首页换肤的图片上传 API 做图床
    application/json和application/x-www-form-urlencoded使用选择
    通过类名或者jar名查询所在jar包
    sql中with as测试实例
    BeanUtils.copyProperties的简单示例
    jquery.validate.js的简单示例
    springboot restful接口服务异常处理
    怎样判断某个窗口是否打开
    怎样理解window.name
  • 原文地址:https://www.cnblogs.com/CSU3901130321/p/4421136.html
Copyright © 2011-2022 走看看