HDU 1800 hash 找出现最多次数的字符串的次数
2015-04-13 00:59
411 查看
乘法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;
}
这类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[105]; int _hash[5000]; int Hash(char *str) { while(*str == '0') str++; //这道题目的字符串要去除前导0 int seed = 131 , value=0; while(*str != '\0'){ value = value*seed+(*str++); } return value; } int main() { // freopen("a.in" , "r" , stdin); int n; while(~scanf("%d" , &n)) { memset(_hash , 0 , sizeof(_hash)); for(int i=0 ; i<n ; i++) { scanf("%s" , str); int index = Hash(str); // cout<<"index: "<<i<<" "<<index<<endl; _hash[i]=index; } sort(_hash , _hash+n); int ans = 0; int cnt = 1; for(int i=1 ; i<n ; i++){ if(_hash[i] == _hash[i-1]){ cnt++; } else{ ans = max(ans , cnt); cnt = 1; } } ans = max(ans , cnt); printf("%d\n" , ans); } return 0; }
相关文章推荐
- 字符串中出现最多次数的字母和次数
- hdu 1800 字符串的hash(BKDRHash 模版)大牛推荐的函数
- HDU 1800 Flying to the Mars(字符串Hash)
- hdu 1004-统计出现次数最多的字符串
- HDU 1004.Let the Balloon Rise【找出出现次数最多的字符串】
- hdu 题目1800 Flying to the Mars (Hash字符串 应用)
- HDU 1800 简单贪心 13.1.10用trie重做 ---其实是统计出现次数最多的单词
- 一个字符串中可能包括a-z中的多个字符,如有重复,求出现最多次数的那个字符及次数,如有多个重复的则都求出
- HDU 1800 Flying to the Mars 【字符串hash(ELFhash算法) / map】
- HDU 3336 Count the string (求前缀在字符串中出现的次数和)
- LA-4513 - Stammering Aliens-(hash字符串+二分答案+hash排序) 找出子串出现次数
- hdu 4552 求所有字符串前缀在字符串中出现的总次数
- HDU 1800 Flying to the Mars 字符串 hash
- 给一段字符串,找出其中出现次数最多的字母及出现的次数(出现最多次数的项只有一个的情况)
- 找出一个字符串里面出现最多次数的字母以及次数
- hdu 1800 字符串水题 可用字符串hash 字典树做 我用了最水的排序水过
- hash数组快速查找一个字符串中出现最多的字符,并统计出现的次数
- CSU-ACM2017暑期训练10-并查集&&HASH F - Flying to the Mars HDU - 1800(字符串hash)
- java程序题:计算字符串中子字符串出现的次数
- 求一个字符串中连续出现次数最多的子串