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; }