码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1002:487-3279(水题,提高题 / hash)

时间:2014-06-04 21:08:17      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

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

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

Sample Output

310-1010 2
487-3279 4
888-4567 3

Source

 
 
  水题,提高题
 
  当时做的时候这道题WA了好多次,原因就是以下注意里面写的情况没有考虑到。这道题没有什么算法,但是考验基本功,可以作为算法入门的提高题来练习。另外这道题有很多种优化方法(据说可以用hash??),看Status中各位大神提交的代码速度就可以知道。我这个600+MS的简直就是渣渣……抽空优化下。参考优化链接:POJ1002-487-3279
 
  题意:给你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的形式可能会超时。
 
  代码
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <stdio.h>
 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     else
27         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]++;   //次数加1
43     }
44     int f=false; //有无重复
45     for(i=0;i<MAXN;i++) //输出
46         if(a[i]>1){
47             f=true;
48             printf("%03d-%04d %d\n",i/10000,i%10000,a[i]);
49         }
50     if(!f)   cout<<"No duplicates."<<endl;
51     return 0;
52 }
bubuko.com,布布扣

 

Freecode : www.cnblogs.com/yym2013

poj 1002:487-3279(水题,提高题 / hash),布布扣,bubuko.com

poj 1002:487-3279(水题,提高题 / hash)

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/yym2013/p/3759246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!