zoukankan      html  css  js  c++  java
  • HDU 1800 Flying to the Mars (ELFhash算法)

    题意:求某个数字最多出现多少次。

    最简单的哈希。把前道0去掉。(既把04->4)

    #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
    ", res);
        }
        return 0;
    }
  • 相关阅读:
    洛谷 1.5.1 Number Triangles 数字金字塔
    洛谷 Sorting a Three-Valued Sequence 三值的排序
    洛谷 Transformations 方块转换
    POJ 1401 Factorial
    Java面试那些事
    JVM字节码执行引擎
    一个工作三年左右的Java程序员和大家谈谈从业心得
    浅谈volatile关键字
    Java内存模型
    Integer 错误的加锁
  • 原文地址:https://www.cnblogs.com/Fy1999/p/9360242.html
Copyright © 2011-2022 走看看