487-3279
Time Limit: 2000MS |
Memory Limit: 65536K |
|
Total Submissions: 151053 |
Accepted: 25753 |
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(7位十进制) 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
解题思路:
用一个数组number[100000][9]存储通过Change函数转化成标准的格式的电话号码。再用快速排序函数qsort排序,最后比较输出相同的号码和出现的次数。
巧用map[]="22233344455566677778889999",避免字母字符与数字字符之间的转换,同时应注意设置存储临时输入数据的字符素组str的大小,不能设为16,应设大一些,因为输入时可以同时输入多个“-”。
源代码:
2 #include<iostream>
3 #include<stdlib.h>
4 usingnamespace std;
5 char number[100000][9];
6 bool have=false;//标记是否有重复号码,有则为true无则为false
7 int cmp(constvoid* a,constvoid* b)
8 {
9 return strcmp((char*)a,(char*)b);
10 }
11 void Change(int n)//转换成标准格式存储于number数组
12 {
13 int i,j;
14 char str[100]; //需将其设置大一些,否则会WA
15 char map[]="22233344455566677778889999";
16 i=j=0;
17 cin>>str;
18 while(str[i]!='\0')
19 {
20 if(str[i]=='-')
21 {
22 ++i; continue;
23 }
24 if(j==3)
25 {
26 number[n][j]='-';
27 ++j; //插入“-”后需自加,否则下一个字符将存储在下标3位置而不是下标4位置
28 }
29 if(str[i]>='A'&&str[i]<='Z')
30 number[n][j]=map[str[i]-'A'];
31 if(str[i]>='0'&&str[i]<='9')
32 number[n][j]=str[i];
33 ++j;
34 ++i;
35 } number[n][8]='\0';
36 }void Count(int i,int n) //计算重复号码和其出现次数
37 {
38 int j,no;
39 no=1;
40 if(i>=n)
41 return ;
42 while(i<n&&strcmp(number[i],number[i+1])==0)
43 {
44 ++i; ++no;
45 }
46 if(no>1)
47 {
48 have=true;
49 j=0;
50 while(number[i][j]!='\0')
51 {
52 cout<<number[i][j]; ++j;
53 }
54 cout<<""<<no<<endl;
55 }
56 Count(i+1,n);
57 }
58 int main()
59 {
60 int n,i;
61 cin>>n;
62 for(i=0;i<n;++i)
63 Change(i);
64 qsort(number,n,sizeof(number[0]),cmp); //快速排序函数
65 Count(0,n);
66 if(have==false)
67 cout<<"No duplicates."<<endl;
68 return0;
69 }
8158526 | PM489 | 1002 | Accepted | 1144K | 797MS | C++ | 1095B | 2011-02-09 15:12:52 |