博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 1002:487-3279(水题,提高题 / hash)
阅读量:4584 次
发布时间:2019-06-09

本文共 4591 字,大约阅读时间需要 15 分钟。

487-3279
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 236746   Accepted: 41288

Description

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. 

Input

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. 

Output

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. 

Sample Input

124873279ITS-EASY888-45673-10-10-10888-GLOPTUT-GLOP967-11-11310-GINOF101010888-1200-4-8-7-3-2-7-9-487-3279

Sample Output

310-1010 2487-3279 4888-4567 3

Source


 
 
  水题,提高题
 
  当时做的时候这道题WA了好多次,原因就是以下注意里面写的情况没有考虑到。这道题没有什么算法,但是考验基本功,可以作为算法入门的提高题来练习。另外这道题有很多种优化方法(据说可以用hash??),看Status中各位大神提交的代码速度就可以知道。我这个600+MS的简直就是渣渣……抽空优化下。参考优化链接:
 
  题意:给你n个格式不同电话号码,你需要统计出其中出现了一次以上的电话号码的次数,并按字典序输出。如果没有号码出现了一次以上,则输出"No duplicates."。
 
  思路:首先写好输入框架,然后把映射函数写出来,最后就是统计次数了。你可以把每一个电话号码转换成一个整数,然后用一个数组记录它出现的次数,数组的下标代表电话号码,对应的数组的值代表出现的次数。这样输出的时候从头到尾遍历一遍找出次数>1的号码输出即可。
  这样做的优点是不用排序,缺点是速度慢(因为号码有7位,所以数组开到了1e7,从头到尾遍历一遍很慢)。
 
  注意
  1.输出的时候注意输出前导0,printf("%03d-%04d %d\n",i/10000,i%10000,a[i]);
  2.别忘了处理"No duplicates."的情况。
  3.用cin,cout的形式可能会超时。
 
  代码
1 #include 
2 #include
3 using namespace std; 4 #define MAXN 10000000 5 int a[MAXN+1]={
0}; 6 int Map(char c) //映射关系 7 { 8 if('0'<=c && c<='9') 9 return c-'0';10 else if(c=='A' || c=='B' || c=='C')11 return 2;12 else if(c=='D' || c=='E' || c=='F')13 return 3;14 else if(c=='G' || c=='H' || c=='I')15 return 4;16 else if(c=='J' || c=='K' || c=='L')17 return 5;18 else if(c=='M' || c=='N' || c=='O')19 return 6;20 else if(c=='P' || c=='R' || c=='S')21 return 7;22 else if(c=='T' || c=='U' || c=='V')23 return 8;24 else if(c=='W' || c=='X' || c=='Y')25 return 9;26 else27 return -1;28 }29 int main()30 {31 char s[111],c;32 int i,j,n;33 scanf("%d%c",&n,&c);34 for(i=1;i<=n;i++){ //输入n个数35 scanf("%s",s);36 int tel = 0;37 for(j=0;s[j];j++){38 int t = Map(s[j]);39 if(t==-1) continue;40 tel = tel*10+t;41 }42 a[tel]++; //次数加143 }44 int f=false; //有无重复45 for(i=0;i
1){47 f=true;48 printf("%03d-%04d %d\n",i/10000,i%10000,a[i]);49 }50 if(!f) cout<<"No duplicates."<

 

Freecode :

转载于:https://www.cnblogs.com/yym2013/p/3759246.html

你可能感兴趣的文章
[18/11/21] 方法
查看>>
遍历循环
查看>>
iframe跨域解决方案
查看>>
函数名后const
查看>>
jetty加载spring-context容器源码分析
查看>>
方向ajax(http long request实现实时通信)
查看>>
[LeetCode&Python] Problem 746. Min Cost Climbing Stairs
查看>>
如何在DOS窗口复制和粘贴命令
查看>>
总结一下技能
查看>>
css 一张图片做导航
查看>>
个人项目耗时对比记录表
查看>>
评判是人工智能性能的准则
查看>>
XACT_ABORT为默认为off事务发生约束性错误会继续运行,为on发生约束性错误时会rollback...
查看>>
MySQL备份和还原
查看>>
设计模式之建造者模式(简单)
查看>>
BootBox使用
查看>>
B. Pyramid of Glasses
查看>>
数据库设计常考题目简要分析
查看>>
《C++ Primer Plus(第6版)》14章 C++代码复用 - 代码清单14.3
查看>>
C#关于软件界面无响应、BUG报警、程序异常退出等情况的监控和报警
查看>>