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
- HDU 1800 Flying to the Mars ELFhash算法
- HDU 1800 Flying to the Mars 【字符串hash(ELFhash算法) / map】
- hdoj-1800-Flying to the Mars ELFhash模板
- hdu1800 Flying to the Mars ELFHash/BKDHash
- hdu 1800 flying to the mars(贪心算法)
- hdu 1800 Flying to the Mars 贪心算法
- Flying to the Mars (hash) 1800 hdu
- hdu 1800 Flying to the Mars
- Hdu-1800 Flying to the Mars
- hdu 1800 Flying to the Mars
- hdu 1800 Flying to the Mars
- HDU-1800-Flying to the Mars
- hdu 1800 Flying to the Mars
- hdu 1800 Flying to the Mars
- hdu 1800 Flying to the Mars
- HDU 1800 Flying to the Mars
- HDU 1800 Flying to the Mars STL
- HDU -1800 Flying to the Mars
- 第1章 TCP/IP(1) TCP/IP族、TCP/IP的分层管理、TCP/IP通信传输流
- Cocoapods
- FFmpeg 基本用法
- 做项目使用的工具陆续笔记自用
- 简单的错误记录
- HDU 1800 Flying to the Mars ELFhash算法
- pycharm 运行django服务器端 以及创建app方法
- RESTful服务最佳实践——(二)
- iOS开发之UIlabel多行文字自动换行 (自动折行)
- 日常(真不正经)
- 题目1167:数组排序
- 英语词性的分类及用法详述
- 【九度OJ】题目1474:矩阵幂 解题报告
- 对spring web启动时IOC源码研究