487-3279

Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

题目链接:

sdut:   http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1001

poj:    http://poj.org/problem?id=1002

Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino's by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza Hut by calling their ``three tens'' number 3-10-10-10.

The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows:

A, B, and C map to 2 
D, E, and F map to 3 
G, H, and I map to 4 
J, K, and L map to 5 
M, N, and O map to 6 
P, R, and S map to 7 
T, U, and V map to 8 
W, X, and Y map to 9

There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010.

Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.)

Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number.

输入

The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters.

输出

Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line:

No duplicates.

示例输入

  1. 12
  2. 4873279
  3. ITS-EASY
  4. 888-4567
  5. 3-10-10-10
  6. 888-GLOP
  7. TUT-GLOP
  8. 967-11-11
  9. 310-GINO
  10. F101010
  11. 888-1200
  12. -4-8-7-3-2-7-9-
  13. 487-3279

示例输出

  1. 310-1010 2
  2. 487-3279 4
  3. 888-4567 3
  4.  
  5. 题目大意:给出大写字母和数字,按照一定的规则映射到电话号码(7位),查找所有的的电话号码,若是有重复,则先按照xxx-xxxx的格式输出电话号码(按照字典序),紧接着一个空格,输出重复的次数;要注意,字母QZ不会出现在输入的字符串中,而且,给出的“伪号码”的个数最多可以到100000个。
    解题思路:由于伪号码的数量可能会非常大,如果定义二维数组保存伪号码的话,或许会容易导致超内存,最好的法子是使用哈希查找的方法,用一个数组hash[]保存重复的次数,下标保存电话号码,由于电话号码最长是7位,所以要定义一个10000000大的数组,就是hash[10000000],保存的时候就已经自动排完序了,所以省去了排序的步骤,效率相应也会比较高,最后遍历输出满足要求的电话号码和重复次数即可。(注意,若是使用c++的话无法运行程序,好像是超内存了,改成c就能正常运行了,真心不知道什么原因)
    代码:
  1. #include<stdio.h>
  2. #include<string.h>
  3. int hash[]={};
  4. int zhuanzhi(int n)
  5. {
  6. int sum=,i;
  7. for(i=;i<n;i++)
  8. sum*=;
  9. return sum;
  10. }
  11. int zhuanhuan(char f[])
  12. {
  13. int sum=,i;
  14. int t=strlen(f);
  15. for(i=t-;i>=;i--)
  16. {
  17. sum=sum+(f[i]-'')*zhuanzhi(t-i);
  18. }
  19. return sum;
  20. }
  21. int main()
  22. {
  23. int zong,i,j;
  24. scanf("%d",&zong);
  25. while(zong--)
  26. {
  27. char f[],g[];
  28. scanf("%s",f);
  29. int s=-;
  30. for(i=;f[i]!='\0';i++)
  31. {
  32. switch(f[i])
  33. {
  34. case '':
  35. g[++s]='';
  36. break;
  37. case '':
  38. g[++s]='';
  39. break;
  40. case 'A':case 'B':case 'C':case '':
  41. g[++s]='';
  42. break;
  43. case 'D':case 'E':case 'F':case '':
  44. g[++s]='';
  45. break;
  46. case 'G':case 'H':case 'I':case '':
  47. g[++s]='';
  48. break;
  49. case 'J':case 'K':case 'L':case '':
  50. g[++s]='';
  51. break;
  52. case 'M':case 'N':case 'O':case '':
  53. g[++s]='';
  54. break;
  55. case 'P':case 'R':case 'S':case '':
  56. g[++s]='';
  57. break;
  58. case 'T':case 'U':case 'V':case '':
  59. g[++s]='';
  60. break;
  61. case 'W':case 'X':case 'Y':case '':
  62. g[++s]='';
  63. break;
  64. }
  65. }
  66. g[++s]='\0';
  67. hash[zhuanhuan(g)]++;
  68. }
  69. int count=;
  70. for(i=;i<;i++)
  71. if(hash[i]>)
  72. {
  73. printf("%03d-%04d %d\n",i/,i%,hash[i]);
  74. count++;
  75. }
  76. if(count==)printf("No duplicates.\n");
  77. return ;
  78. }
  1.  

方法二:用c++标准模板库关联容器map做,在sdut上ac了,但是在poj上超时了,不知道问题出在哪里。问题找到了,交的时候要选择“c++”,而默认的提交方式是“g++”,所以要注意这一点才行。

  1. #include<iostream>
  2. #include<string>
  3. #include<map>
  4. using namespace std;
  5. int main()
  6. {
  7. int n;
  8. cin>>n;
  9. map<string,int>mapx;
  10. pair<map<string,int>::iterator,bool>pairx;
  11. int i;
  12. while(n--)
  13. {
  14. string f,temp("");
  15. cin>>f;
  16. int flag=;
  17. for(i=; i<=f.length(); i++)
  18. {
  19. if(flag==)
  20. {
  21. temp.append("-");
  22. flag++;
  23. }
  24. if(f[i]=='A'||f[i]=='B'||f[i]=='C'||f[i]=='')
  25. {
  26. temp.append("");
  27. flag++;
  28. }
  29. else if(f[i]=='D'||f[i]=='E'||f[i]=='F'||f[i]=='')
  30. {
  31. temp.append("");
  32. flag++;
  33. }
  34. else if(f[i]=='G'||f[i]=='H'||f[i]=='I'||f[i]=='')
  35. {
  36. temp.append("");
  37. flag++;
  38. }
  39. else if(f[i]=='J'||f[i]=='K'||f[i]=='L'||f[i]=='')
  40. {
  41. temp.append("");
  42. flag++;
  43. }
  44. else if(f[i]=='M'||f[i]=='N'||f[i]=='O'||f[i]=='')
  45. {
  46. temp.append("");
  47. flag++;
  48. }
  49. else if(f[i]=='P'||f[i]=='R'||f[i]=='S'||f[i]=='')
  50. {
  51. temp.append("");
  52. flag++;
  53. }
  54. else if(f[i]=='T'||f[i]=='U'||f[i]=='V'||f[i]=='')
  55. {
  56. temp.append("");
  57. flag++;
  58. }
  59. else if(f[i]=='W'||f[i]=='X'||f[i]=='Y'||f[i]=='')
  60. {
  61. temp.append("");
  62. flag++;
  63. }
  64. else if(f[i]=='')
  65. {
  66. temp.append("");
  67. flag++;
  68. }
  69. else if(f[i]=='')
  70. {
  71. temp.append("");
  72. flag++;
  73. }
  74. }
  75. pairx=mapx.insert(pair<string,int>(temp,));
  76. if(pairx.second==)
  77. {
  78. ++pairx.first->second;
  79. }
  80. else continue;
  81. }
  82. int flag2=;
  83. map<string,int>::iterator p;
  84. for(p=mapx.begin(); p!=mapx.end(); p++)
  85. {
  86. if(p->second==)
  87. continue;
  88. else
  89. {
  90. cout<<p->first<<" "<<p->second+<<endl;
  91. flag2=;
  92. }
  93. }
  94. if(flag2==)
  95. {
  96. cout<<"No duplicates."<<endl;
  97. }
  98. return ;
  99. }
  1.  

sdut 487-3279【哈希查找,sscanf ,map】的更多相关文章

  1. QMap QHash的选择(QString这种复杂的比较,哈希算法比map快很多)

    QMap QHash有近乎相同的功能.很多资料里面介绍过他们之间的区别了.但是都没有说明在使用中如何选择他们. 实际上他们除了存储顺序的差别外,只有key操作的区别. 哈希算法是将包含较多信息的“ke ...

  2. 查找算法(7)--Hash search--哈希查找

    1.哈希查找 (1)什么是哈希表(Hash) 我们使用一个下标范围比较大的数组来存储元素.可以设计一个函数(哈希函数, 也叫做散列函数),使得每个元素的关键字都与一个函数值(即数组下标)相对应,于是用 ...

  3. 数据结构与算法之PHP查找算法(哈希查找)

    一.哈希查找的定义 提起哈希,我第一印象就是PHP里的关联数组,它是由一组key/value的键值对组成的集合,应用了散列技术. 哈希表的定义如下: 哈希表(Hash table,也叫散列表),是根据 ...

  4. python数据结构与算法 29-1 哈希查找

    ).称为哈希查找. 要做到这种性能,我们要知道元素的可能位置.假设每一个元素就在他应该在的位置上,那么要查找的时候仅仅须要一次比較得到有没有的答案,但以下将会看到.不是这么回事. 到10. water ...

  5. python 哈希查找

    import random INDEXBOX= #哈希表元素个数 MAXNUM= #数据个数 class Node: #声明链表结构 def __init__(self,val): self.val= ...

  6. hash 哈希查找复杂度为什么这么低?

    hash 哈希查找复杂度为什么这么低? (2017-06-23 21:20:36) 转载▼   分类: c from: 作者:jillzhang 出处:http://jillzhang.cnblogs ...

  7. SDUT 3379 数据结构实验之查找七:线性之哈希表

    数据结构实验之查找七:线性之哈希表 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 根据给定 ...

  8. SDUT 3377 数据结构实验之查找五:平方之哈希表

    数据结构实验之查找五:平方之哈希表 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 给定的一组 ...

  9. [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map

    13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...

随机推荐

  1. 顺序栈的c++实现及利用其实现括号的匹配

    #include<iostream>#include<cassert>#include<cstring>#include<string>using na ...

  2. CCF 模拟D 动态规划

    http://115.28.138.223:81/view.page?opid=4 这道题写的我醉醉的,想建一棵指定深度的树最后统计满足条件的个数 居然没去考虑这样必然超时!!!代码写的也是醉了,把没 ...

  3. COGS 2434 暗之链锁 题解

    [题意] 给出一个有n个点的无向图,其中有n-1条主要边且这些主要边构成一棵树,此外还有m条其他边,求斩断原图的一条主要边和一条其他边使得图不连通的方案数. 注意,即使只斩断主要边就可以使得原图不连通 ...

  4. espcms自定义表单邮件字段

    /include/inc_replace_mailtemplates.php中增加一行就可以了. 如:$replacemail['mailform'][] = array(name => '职位 ...

  5. SSRS报表参数设置

    一.日期时间类型的参数注意事项: 关于数据类型的选择:(只有数据类型设置为日期/时间格式,在查询的时候才会显示日期控件,提示信息一般改成汉字) 指定默认值:指定开始日期为前10天,

  6. Reorder array to construct the minimum number

    Construct minimum number by reordering a given non-negative integer array. Arrange them such that th ...

  7. php如何妩媚地生成执行的sql语句

    会不会碰到这样一种情况呢?每次获取数据将数据和历史版本都有一定的差别,然而用ThinkPHP的addAll()函数,却会将已有的数据删掉再重新写入.这明显不是我们想要的.但自己写sql每次几十个字段也 ...

  8. XE 的程序升级 XE5 问题处理记录

    XE 的程序升级 XE5 问题处理记录 1.  [dcc32 Fatal Error] frxClass.pas(3556): F1026 File not found: 'xxxxx\Registr ...

  9. MongoDB 查询优化分析

    摘要: 在MySQL中,慢查询日志是经常作为我们优化查询的依据,那在MongoDB中是否有类似的功能呢?答案是肯定的,那就是开启Profiling功能.该工具在运行的实例上收集有关MongoDB的写操 ...

  10. Mac会给你一些欣喜

    Mac会给你一些欣喜 以前一直没有用过Mac,一直都是用Windows的电脑,只是偶尔会去用Ubuntu这样的Linux系统.Mac OS 确实是一只可以给你欣喜的系统. 上周拿到公司分发的Mac,到 ...