Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 227999 | Accepted: 39752 |
Description
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
Output
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
解題思路:
這個問題,其實就是對字符串的處理問題,先將輸入的字符串,轉換成相應的電話號碼。這裏因爲轉換並不是規則的,所以可以用一個map來進行轉換。
得用qsort來進行快速排序。
代碼:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char map[]="22233344455566677778889999";
char num[100000][9];
char *convertToNum(char *str){
char temp[9];
int i=0,k=0;
for(i=0;i<strlen(str);i++){
if(str[i]=='-')
continue;
if(str[i]>='A'&& str[i]<='Z')
temp[k]=map[str[i]-'A'];
else
temp[k]=str[i];
k++;
if(k==3){
temp[k++]='-';
}
}
temp[k]='\0';
strcpy(str,temp);
return str;
}
int compare(const void *ele1,const void *ele2){
return strcmp((char*)ele1,(char*)ele2);
}
int main(void){
int nCases,i,j;
char temp[80];
int noDuplicates;
scanf("%d",&nCases);
for(i=0;i<nCases;i++){
scanf("%s",temp);
strcpy(num[i],convertToNum(temp));
}
qsort(num,nCases,9,compare);
noDuplicates=1;
for(i=0;i<nCases;i++){
for(j=i+1;j<nCases && strcmp(num[i],num[j])==0;j++);
if(j-i>1){
printf("%s %d\n",num[i],j-i);
noDuplicates=0;
}
i=j-1;
}
if(noDuplicates)
printf("No duplicates.\n");
return 0;
}