乘法hash:

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

int Hash(char *str)
{
    int seed = 131 , value=0;
    while(*str != '\0'){
        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 != '\0'){
        hash = hash*a+(*str++);

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

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
char str[];
int _hash[]; int Hash(char *str)
{
while(*str == '') str++; //这道题目的字符串要去除前导0
int seed = , value=;
while(*str != '\0'){
value = value*seed+(*str++);
}
return value;
} int main()
{
// freopen("a.in" , "r" , stdin);
int n;
while(~scanf("%d" , &n))
{
memset(_hash , , sizeof(_hash));
for(int i= ; i<n ; i++)
{
scanf("%s" , str);
int index = Hash(str);
// cout<<"index: "<<i<<" "<<index<<endl;
_hash[i]=index;
}
sort(_hash , _hash+n);
int ans = ;
int cnt = ;
for(int i= ; i<n ; i++){
if(_hash[i] == _hash[i-]){
cnt++; }
else{
ans = max(ans , cnt);
cnt = ;
}
}
ans = max(ans , cnt);
printf("%d\n" , ans);
}
return ;
}

最新文章

  1. Basic Tutorials of Redis(7) -Publish and Subscribe
  2. 修改MySQL中字段的类型和长度
  3. IOS开发之不同版本适配问题2(#ifdef __IPHONE_7_0)
  4. 屌丝IT男
  5. I.MX6 隐藏电池图标
  6. bzoj2324后续思考
  7. C#扫盲之:==/Equals /ReferenceEquals 异同的总结,相等性你真的知道吗?
  8. HDU5627--Clarke and MST (bfs+位运算)
  9. C#打印条码BarTender SDK打印之路和离开之路(web平凡之路)
  10. android NDK jni下的c文件 Unresolved inclusion
  11. cocos2d-x3.0之请求网络(phpserver)
  12. 第2章Zabbix基础进阶
  13. span是没有value标签的,要向获得标签内部的值改怎么办。
  14. Kirill And The Game CodeForces - 842A
  15. CSS _text-align:justify;实现两端对齐
  16. vue.js实战——vue元素复用
  17. IP通信基础学习第六周(上)
  18. vue-cli 最强指南
  19. react全家桶-服务端与客户端配置
  20. 升级java编译器

热门文章

  1. MyEclipse2014+Maven配置记录
  2. electron打包整理
  3. get和post中文乱码原理相关博客
  4. Robot Framework and Ride
  5. 微信小程序之多行文本省略号
  6. Shiro 自定义登陆、授权、拦截器
  7. QT入门学习
  8. Python学习 Day 4 函数 切片 迭代 列表生成式 生成器
  9. Ajax请求WebService跨域问题
  10. jQuery 点击 星星评分