【邻接表字符串Hash】【HDU1800】Flying to the Mars
题意:
给你N个数字,带前导0,问出现最多的数字个数
思路:
读入,清楚前导0,Hash。
用邻接表字符串Hash有一下几点注意
string,不要memset,否则地址也没了,涉及到stl的东西,少用memset,类似还有vector;
用
- assign截断字符串也是很黄很暴力的
- 直接assign(a.begin(),a.end) 即可
其实这种算法跟MAP 也没什么两样了。。。字符串HASH的更强大地方在于后缀的随机算法,这里不要过于纠结
而且MAP 效率高的多。。。
- #include <cstdio>
- #include <cstdlib>
- #include <cmath>
- #include <cstring>
- #include <ctime>
- #include <algorithm>
- #include <iostream>
- #include <sstream>
- #include <string>
- #define oo 0x13131313
- using namespace std;
- int N;
- struct edge
- {
- string k;
- edge *next;
- int ans;
- }E[4000];
- struct node
- {
- edge *first;
- }Hash[10000];
- int ans=1;
- void inputandhash()
- {
- ans=1;
- int tot=0;
- memset(Hash,0,sizeof(Hash));
- string a;
- for(int i=1;i<=N;i++)
- {
- int temp=0,ok=1,p=0;
- cin>>a;
- for(int j=0;j<a.size();j++)
- {
- if(a[j]!='0') break;
- else p=j+1;
- }
- a.assign(a.begin()+p,a.end());
- for(int j=0;j<a.size();j++)
- {
- temp=(temp+(j+1)*(a[j]-'0'))%10000;
- }
- for(edge *p=Hash[temp].first;p;p=p->next)
- {
- if((p->k)==a) {
- p->ans++;
- ok=0;
- if(p->ans>ans) ans=p->ans;
- break;
- }
- }
- if(ok)
- {
- E[tot].k.assign(a);
- E[tot].ans=1;
- E[tot].next=Hash[temp].first;
- Hash[temp].first=E+tot;
- tot++;
- }
- }
- }
- void init()
- {
- freopen("a.in","r",stdin);
- freopen("a.out","w",stdout);
- }
- int main()
- {
- // init();
- while(scanf("%d",&N)!=EOF)
- {
- inputandhash();
- printf("%d\n",ans);
- }
- return 0;
- }
【邻接表字符串Hash】【HDU1800】Flying to the Mars的更多相关文章
- hdu---(1800)Flying to the Mars(trie树)
Flying to the Mars Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU1800 Flying to the Mars 【贪心】
Flying to the Mars Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- nyoj--138--找球号(二)(hash+邻接表)
找球号(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中,每个球上都有一个整数编号i(0<=i<=1 ...
- Berkeley DB的数据存储结构——哈希表(Hash Table)、B树(BTree)、队列(Queue)、记录号(Recno)
Berkeley DB的数据存储结构 BDB支持四种数据存储结构及相应算法,官方称为访问方法(Access Method),分别是哈希表(Hash Table).B树(BTree).队列(Queue) ...
- 纸上谈兵:哈希表(hash table)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! HASH 哈希表(hash table)是从一个集合A到另一个集合B的映射(map ...
- 各种字符串Hash函数比较(转)
常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...
- 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774
Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...
- topo排序 + 用邻接表优化后的
输入数据: 4 61 21 32 33 42 44 2 4 61 21 32 33 42 41 2 topo排序为偏序: #include<stdio.h> #include<que ...
- 数据结构 《2》----基于邻接表表示的图的实现 DFS(递归和非递归), BFS
图通常有两种表示方法: 邻接矩阵 和 邻接表 对于稀疏的图,邻接表表示能够极大地节省空间. 以下是图的数据结构的主要部分: struct Vertex{ ElementType element; // ...
随机推荐
- [x-means] 1.x-means简介
本文基于<X-means>和<BIC-notes>(原论文中BIC公式有误,这是对BIC的补充) K-means的缺点 每一轮迭代的计算花费大 需要用户指定K 易于收敛到局部最 ...
- C errors recods
error: unterminated #ifndef 1,权限问题 2,少了#endif
- Curly braces in Python in 2012? - Stack Overflow
Curly braces in Python in 2012? - Stack Overflow Curly braces in Python in 2012? [closed]
- Tengine笔记2:通过IP、域名、端口实现虚拟主机
一.通过端口创建虚拟主机 案例:通过端口访问两个不同的页面 将/usr/local/tengine-2.1.0/html/index.html内的内容改为 Welcom to port1 然后在/op ...
- Mobile Web开发 处理设备的横竖屏
为了应对移动设备屏幕的碎片化,我们在开发Mobile Web应用时,一个最佳实践就是采用流式布局,保证最大可能地利用有限的屏幕空间.由于屏幕存在着方向性,用户在切换了屏幕的方向后,有些设计上或实现上的 ...
- mysql插入大量数据
创建实验表: CREATE TABLE `a` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(50) NOT NULL, `type` ch ...
- string.Format组合跳转路径
string url = this.ResolveClientUrl("~/page/bn_stu.aspx"); string str = string.Format(" ...
- Java学习—— for循环
For双重循环 /* 循环语句嵌套 */ class ForForTest { public static void main(String[] args) { /*int x,y = 0; for( ...
- 04JS高级动态添加属性和删除属性
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- apple程序生命周期
iphone程序的生命周期分析 做iphone开发首先第一件就是得知道iphone程序的生命周期,说白点就是当点击程序图标启动程序开始到退出程序整个使用运行过程中底下的代码都发生了什么,只有理解了这个 ...