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