Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1951467
  • 博文数量: 261
  • 博客积分: 8073
  • 博客等级: 中将
  • 技术积分: 2363
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-10 15:23
文章分类

全部博文(261)

文章存档

2013年(1)

2012年(1)

2011年(50)

2010年(34)

2009年(4)

2008年(17)

2007年(55)

2006年(99)

分类:

2007-06-13 10:18:27

题目:

487-3279
Time Limit:2000MS  Memory Limit:30000K
Total Submit:47133 Accepted:8115

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

程序
#include 
#include
#include

#define MAXBUFSIZE 20
typedef struct phoneNum{
char normal[MAXBUFSIZE];
char standard[MAXBUFSIZE];
}PhoneNum;

int getInput();
void convertToStandard(PhoneNum *pn, int start, int end);
char map(char ch);
void print(PhoneNum *pn, int start, int end);
void sort(PhoneNum *pn, int left, int right);
int median(PhoneNum *pn, int left, int right);
void swap(PhoneNum *pn, int idx1, int idx2);
void printResult(PhoneNum *pn, int start, int end);

int main()
{
int i, count;
PhoneNum *pn; //items of phonenum
scanf("%d", &count);
if(count <= 0 || count > 100000)
return;
if((pn = (PhoneNum *)malloc(sizeof(PhoneNum) * count)) == NULL){
printf("allocation error\n");
return;
}
for(i = 0;i < count;i++)
scanf("%s", pn[i].normal);
convertToStandard(pn, 0, count - 1);
sort(pn, 0, count - 1);
printResult(pn, 0, count - 1);
}

void convertToStandard(PhoneNum *pn, int start, int end)
{
int i, j, k;
char c;
for(i = start;i <= end;i++){
k = 0;
for(j = 0;j < strlen(pn[i].normal);j++){
c = pn[i].normal[j];
if(c == '-')
continue;
if(k == 3)
pn[i].standard[k++] = '-';
pn[i].standard[k++] = map(c);
}
pn[i].standard[k] = '\0';
}
}
char map(char ch)
{
static char n;
if(ch == '1')
n = '1';
else if(ch == 'A' || ch == 'B' || ch == 'C' || ch == '2')
n = '2';
else if(ch == 'D' || ch == 'E' || ch == 'F' || ch == '3')
n = '3';
else if(ch == 'G' || ch == 'H' || ch == 'I' || ch == '4')
n = '4';
else if(ch == 'J' || ch == 'K' || ch == 'L' || ch == '5')
n = '5';
else if(ch == 'M' || ch == 'N' || ch == 'O' || ch == '6')
n = '6';
else if(ch == 'P' || ch == 'R' || ch == 'S' || ch == '7')
n = '7';
else if(ch == 'T' || ch == 'U' || ch == 'V' || ch == '8')
n = '8';
else if(ch == 'W' || ch == 'X' || ch == 'Y' || ch == '9')
n = '9';
else if(ch == '-')
n = '-';
else
n = ch;
return n;
}

void print(PhoneNum *pn, int start, int end)
{
int i;
if(start > end)
return;
for(i = start;i <= end;i++){
printf("PhoneNum[%d].normal = %s\t", i, pn[i].normal);
printf("PhoneNum[%d].standard = %s\n", i, pn[i].standard);
}
}

void sort(PhoneNum *pn, int left, int right)
{
if(left + 2 <= right){
int i, j, pivot;
pivot = median(pn, left, right);
i = left;
j = right - 1;
while(1){
while(strcmp(pn[++i].standard, pn[pivot].standard) < 0);
while(strcmp(pn[--j].standard, pn[pivot].standard) > 0);
if(i < j)
swap(pn, i, j);
else
break;
}
swap(pn, i, right - 1);
sort(pn, left, i - 1);
sort(pn, i + 1, right);
}
else if(left + 1 == right){
if(strcmp(pn[left].standard, pn[right].standard) > 0)
swap(pn, left, right);
}
else
return;
}

int median(PhoneNum *pn, int left, int right)
{
int center;
center = (left + right)/2;
if(strcmp(pn[center].standard, pn[left].standard) < 0)
swap(pn, center, left);
if(strcmp(pn[right].standard, pn[left].standard) < 0)
swap(pn, right, left);
if(strcmp(pn[right].standard, pn[center].standard) < 0)
swap(pn, right, center);
swap(pn, center, right - 1);
return (right - 1);
}

void swap(PhoneNum *pn, int idx1, int idx2)
{
char normal[MAXBUFSIZE];
char standard[MAXBUFSIZE];
strcpy(normal, pn[idx1].normal);
strcpy(standard, pn[idx1].standard);
strcpy(pn[idx1].normal, pn[idx2].normal);
strcpy(pn[idx1].standard, pn[idx2].standard);
strcpy(pn[idx2].normal, normal);
strcpy(pn[idx2].standard, standard);

}
void printResult(PhoneNum *pn, int start, int end)
{
int i, j, count, flag = 0;
for(i = start;i < end;i = j){
count = 1;
for(j = i + 1;strcmp(pn[j].standard, pn[i].standard) == 0;j++){
count++;
}
if(count > 1){
flag = 1;
printf("%s %d\n",pn[i].standard, count);
}
}
if(!flag)
printf("No duplicates\n");
}
结果
Time Limit Exceed

阅读(1677) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~