hdu1800(哈希或map)
来源:互联网 发布:湖南省2016年经济数据 编辑:程序博客网 时间:2024/06/06 04:49
题意大概就是求一些重复出现的字符串中最大的次数,用map可以水过,但是时间比较慢,rp不好就会TLE,看服务器洛,正好学了了哈希,用哈希搞是绝对不会超时的
写贴个map写法,998ms,760,670还TLE了一次,也是醉了
#include <iostream>#include <cstring>#include <queue>#include <cstdio>#include <algorithm>#include <string>#include <map>using namespace std;int main(){#ifdef xxz freopen("in.txt","r",stdin);#endif int n; while(cin>>n) { map<string,int> mp; for(int i = 0; i < n; i++) { string a; cin>>a; for(string::iterator its = a.begin(); *its == '0'; a.erase(its) );//要把前导0去掉,不然得到的哈希值不同 mp[a]++; } map<string,int>::iterator it; int sum = 0; for(it = mp.begin(); it != mp.end(); it++) { sum = max(sum,it->second); } cout<<sum<<endl; } return 0;}
#include<iostream>#include<limits.h>#include<string.h>#include<string>#include<algorithm>#include<stdio.h>using namespace std;int N;int Hash[3010];char str[40];int BKDRHash(char* s){ long long seed=131; long long hash=0; while(*s=='0')s++;//这点很重要,因为0000345645和34564是一个数,但是如果不处理的话会对应到不同的Hash值 while(*s) { hash=hash*seed+(*s++); } return (hash & 0x7FFFFFFF);}int main(){ int i,ans; while(scanf("%d",&N)!=EOF) { i=0,ans=1; for(i=0;i<N;i++) { scanf("%s",str); Hash[i]=BKDRHash(str); } sort(Hash,Hash+N); int temp=1; for(i=1;i<N;i++)//ans即为 相等的数的最大值 { if(Hash[i]==Hash[i-1]) { temp++; if(temp>ans)ans=temp; } else temp=1; } printf("%d\n",ans); } return 0;}
0 0
- hdu1800(哈希或map)
- 字典树+map+hdu1800
- hdu1800 map统计
- HDU1800——Flying to the Mars(map+贪心)
- hdu1800 Flying to the Mars(map水)
- HDU1800
- HDU1800
- hdu1800 Flying to the Mars(map)
- hud2648 -哈希或map
- Flying to the Mars HDU1800 map 简单水题
- 哈希函数--字符串哈希--hdu1800 Flying to the Mars
- HDU1800 Flying to the Mars(贪心)
- Flying to the Mars(hdu1800,水题排序)
- hdu1800 Flying to the Mars(字典树)
- hdu1800(hash)
- codevs3004 我是世界之王 ——map或字符串哈希或字典序
- hdu1800 字典树
- hdu1800 排序二分法nlogn
- VS2010中常用的快捷键
- python实现简单爬虫功能
- JVM垃圾收集器介绍
- BibTex学习
- 关于opencv2.4.9和VS2013在windows7下无法打开摄像头的问题的扯淡解决办法
- hdu1800(哈希或map)
- 黑马程序员——Java基础——IO流
- 数据结构(c++版)第五章 树和二叉树
- 非递归遍历 二叉树 前序 中序 后序 通用模板
- 最优最小权匹配
- 调用系统相册 发现 状态栏消失
- CCS新建Cortex-M4芯片工程详解及说明
- Hibernate中的left outer join
- 前缀、中缀、后缀表达式