487-3279
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 283323 | Accepted: 50852 |
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.
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.
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
Analyze Descrisption
1.第一行输入电话号码的个数。
2.每个电话号码有7位(数字或字母或两者混合),标准格式是111-1111.
3.输出格式按照首字母升序,若首字母相同,则看次字母升序,同理往下进行。
4.若所有号码都不匹配,则输出No duplicates.
5.本题考了:排序,字符串读取转换,字符串比较。
Code Presentation
方法1:这个方法是我在百度上看到的,代码写的特别漂亮简洁:
#include <stdio.h>
#define M 10000000
int i,n,k,j,b[M];
char s[99],c;
int main()
{
for(scanf("%d",&n);i++<n;)//输入电话号码的数量n
{
scanf("%s",s);//用数组存储电话号码
for(k=j=0;s[j];)//用s[j]判断是否找遍数组里所有字符
{
c=s[j++];//处理单个字符
if(c!=81&&c!=90&&c!=45)// ASCII 'Q'=81 'Z'=90 '-'=45 大写字母的ASCII值在65-90之间 'P'=80
k=k*10+(64<c?(c-(80<c)-59)/3:c-48);//c>64字符c是大写字母,c>80代表P之后的大写字母,k的值为 对应的数字
} //c<64是数字
b[k]++;//用数组b存储转化来的电话号码
}
for(i=0;i<M;i++)//遍历b数组
if(1<b[i])//电话号码转换的数字是从2开始的
{
n=i;
s[3]=45;//第四位设置成分隔符
for(k=s[8]=j=0;j<7;j++)//第九位置零
{
s[6-j+(j<4)]=n%10+48;
n/=10;
}
printf("%s %d\n",s,b[i]);//有相同的号码,则输出号码以及出现次数
}
if(k)printf("No duplicates.\n");
return 0;
}
还有一种是用hash做的,因为我之前没接触到过hash,所以看了半天才看懂,顺便也当学习hash了
#include <cstdio>
#include <algorithm>
using namespace std;
char s[31];
int Hash()
{
int sum=0;
for(int i=0,k=0;k<7;i++)
{
if(s[i]>='0'&&s[i]<='9')//数字保留
{
sum*=10;k++;
sum+=(s[i]-'0');
}
else if(s[i]>='A'&&s[i]<'Z')//字母转换成数字
{
sum*=10;k++;
sum+=((s[i]-'A'-(s[i]>'Q'))/3+2);
}
}
return sum;
}
int main()
{
int n;scanf("%d",&n);//输入电话号码的数量
int data[n];getchar();
for(int tmp=0;tmp<n;tmp++)
{
gets(s);//输入号码,存储在s数组里
data[tmp]=Hash();//存储第一个号码的键值
}
sort(data,data+n);//将映射后的值排序
bool p=false;n--;
for(int i=0,num=1;i<n;i+=num=1)
{
while(data[i]==data[i+1])//比较号码的hash值是否相同
{
num++;//号码重复次数
i++;
}
if(num>1)
{
printf("%03d-%04d %d\n",data[i]/10000,data[i]%10000,num);//逆解
p=true;
}
}
if(!p)printf("No duplicates.\n");
return 0;
}