HDU 1800 Flying to the Mars ELFhash算法

来源:互联网 发布:java小项目源代码 编辑:程序博客网 时间:2024/05/22 13:05

题目:

http://acm.hdu.edu.cn/showproblem.php?pid=1800

题意:

给出一些数字,求出这些数字中出现次数,输出最多的那个次数。其中数字不超过30位

思路:

数字范围较大,只能用字符串储存,注意去掉前导0,用map或其他的hash算法都可以,这里用的ELFhash算法

#include <bits/stdc++.h>using namespace std;typedef unsigned int ui;const int N = 7003, MOD = 7003;int Hash[N], num[N];int res;int ELFhash(char *str)//思想就是一直杂糅,使字符之间互相影响{    ui h = 0, g;    while(*str)    {        h = (h<<4) + *str++; //h左移4位,当前字符占8位,加到h中进行杂糅        if((g = h & 0xf0000000) != 0) //取h最左四位的值,若均为0,则括号中执行与否没区别,故不执行        {            h ^= g>>24; //用h的最左四位的值对h的右起5~8进行杂糅            h &= ~g;//清空h的最左四位        }    }    return h; //因为每次都清空了最左四位,最后结果最多也就是28位二进制整数,不会超int}void hash_table(char *str){    int k = ELFhash(str);    int t = k % MOD;    while(Hash[t] != k && Hash[t] != -1) t = (t + 1) % MOD;//开放地址法处理hash    if(Hash[t] == -1) num[t] = 1, Hash[t] = k;    else res = max(res, ++num[t]);}int main(){    int n;    char str[100];    while(~ scanf("%d", &n))    {        getchar();        res = 1;        memset(Hash, -1, sizeof Hash);        for(int i = 1; i <= n; i++)        {            scanf("%s", str);            int j = 0;            while(str[j] == '0') j++;            hash_table(str + j);        }        printf("%d\n", res);    }    return 0;}
0 0
原创粉丝点击