题意:

给你N个数字,带前导0,问出现最多的数字个数

思路:

读入,清楚前导0,Hash。

用邻接表字符串Hash有一下几点注意

string,不要memset,否则地址也没了,涉及到stl的东西,少用memset,类似还有vector;

  1. assign截断字符串也是很黄很暴力的
  1. 直接assign(a.begin(),a.end) 即可

其实这种算法跟MAP 也没什么两样了。。。字符串HASH的更强大地方在于后缀的随机算法,这里不要过于纠结

而且MAP 效率高的多。。。

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <algorithm>
  7. #include <iostream>
  8. #include <sstream>
  9. #include <string>
  10. #define oo 0x13131313
  11. using namespace std;
  12. int N;
  13. struct edge
  14. {
  15. string k;
  16. edge *next;
  17. int ans;
  18. }E[4000];
  19. struct node
  20. {
  21. edge *first;
  22. }Hash[10000];
  23. int ans=1;
  24. void inputandhash()
  25. {
  26. ans=1;
  27. int tot=0;
  28. memset(Hash,0,sizeof(Hash));
  29. string a;
  30.  
  31. for(int i=1;i<=N;i++)
  32. {
  33. int temp=0,ok=1,p=0;
  34. cin>>a;
  35. for(int j=0;j<a.size();j++)
  36. {
  37. if(a[j]!='0') break;
  38. else p=j+1;
  39. }
  40. a.assign(a.begin()+p,a.end());
  41. for(int j=0;j<a.size();j++)
  42. {
  43. temp=(temp+(j+1)*(a[j]-'0'))%10000;
  44. }
  45. for(edge *p=Hash[temp].first;p;p=p->next)
  46. {
  47. if((p->k)==a) {
  48. p->ans++;
  49. ok=0;
  50. if(p->ans>ans) ans=p->ans;
  51. break;
  52. }
  53. }
  54. if(ok)
  55. {
  56. E[tot].k.assign(a);
  57. E[tot].ans=1;
  58. E[tot].next=Hash[temp].first;
  59. Hash[temp].first=E+tot;
  60. tot++;
  61. }
  62. }
  63. }
  64. void init()
  65. {
  66. freopen("a.in","r",stdin);
  67. freopen("a.out","w",stdout);
  68. }
  69. int main()
  70. {
  71. // init();
  72. while(scanf("%d",&N)!=EOF)
  73. {
  74. inputandhash();
  75. printf("%d\n",ans);
  76. }
  77. return 0;
  78. }

【邻接表字符串Hash】【HDU1800】Flying to the Mars的更多相关文章

  1. 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 ...

  2. HDU1800 Flying to the Mars 【贪心】

    Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. nyoj--138--找球号(二)(hash+邻接表)

    找球号(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中,每个球上都有一个整数编号i(0<=i<=1 ...

  4. Berkeley DB的数据存储结构——哈希表(Hash Table)、B树(BTree)、队列(Queue)、记录号(Recno)

    Berkeley DB的数据存储结构 BDB支持四种数据存储结构及相应算法,官方称为访问方法(Access Method),分别是哈希表(Hash Table).B树(BTree).队列(Queue) ...

  5. 纸上谈兵:哈希表(hash table)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! HASH 哈希表(hash table)是从一个集合A到另一个集合B的映射(map ...

  6. 各种字符串Hash函数比较(转)

    常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...

  7. 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774

    Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...

  8. topo排序 + 用邻接表优化后的

    输入数据: 4 61 21 32 33 42 44 2 4 61 21 32 33 42 41 2 topo排序为偏序: #include<stdio.h> #include<que ...

  9. 数据结构 《2》----基于邻接表表示的图的实现 DFS(递归和非递归), BFS

    图通常有两种表示方法: 邻接矩阵 和 邻接表 对于稀疏的图,邻接表表示能够极大地节省空间. 以下是图的数据结构的主要部分: struct Vertex{ ElementType element; // ...

随机推荐

  1. [x-means] 1.x-means简介

    本文基于<X-means>和<BIC-notes>(原论文中BIC公式有误,这是对BIC的补充) K-means的缺点 每一轮迭代的计算花费大 需要用户指定K 易于收敛到局部最 ...

  2. C errors recods

    error: unterminated #ifndef 1,权限问题 2,少了#endif

  3. Curly braces in Python in 2012? - Stack Overflow

    Curly braces in Python in 2012? - Stack Overflow Curly braces in Python in 2012? [closed]

  4. Tengine笔记2:通过IP、域名、端口实现虚拟主机

    一.通过端口创建虚拟主机 案例:通过端口访问两个不同的页面 将/usr/local/tengine-2.1.0/html/index.html内的内容改为 Welcom to port1 然后在/op ...

  5. Mobile Web开发 处理设备的横竖屏

    为了应对移动设备屏幕的碎片化,我们在开发Mobile Web应用时,一个最佳实践就是采用流式布局,保证最大可能地利用有限的屏幕空间.由于屏幕存在着方向性,用户在切换了屏幕的方向后,有些设计上或实现上的 ...

  6. mysql插入大量数据

    创建实验表: CREATE TABLE `a` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(50)  NOT NULL, `type` ch ...

  7. string.Format组合跳转路径

    string url = this.ResolveClientUrl("~/page/bn_stu.aspx"); string str = string.Format(" ...

  8. Java学习—— for循环

    For双重循环 /* 循环语句嵌套 */ class ForForTest { public static void main(String[] args) { /*int x,y = 0; for( ...

  9. 04JS高级动态添加属性和删除属性

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  10. apple程序生命周期

    iphone程序的生命周期分析 做iphone开发首先第一件就是得知道iphone程序的生命周期,说白点就是当点击程序图标启动程序开始到退出程序整个使用运行过程中底下的代码都发生了什么,只有理解了这个 ...