Uva 755 487--3279

 487-3279 

Businesses like to have memorable telephone numbers. One way to make a telephone number memorableis to have it spell a memorable word or phrase. For example, you can call the University of Waterloo bydialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. Whenyou get back to your hotel tonight you can order a pizza from Gino's by dialing 310-GINO. Anotherway to make a telephone number memorable is to group the digits in a memorable way. You couldorder 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 andfourth 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 thestandard 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 thequality control process you want to check that no two (or more) businesses in the directory have the same telephone number.

Input 

The first line of the input contains the number of datasets in the input. A blank line follows. The first line of each dataset specifies the number of telephone numbersin the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list thetelephone numbers in the directory, with each number alone on a line. Each telephone number consistsof a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactlyseven of the characters in the string will be digits or letters. There's a blank line between datasets.

Output 

Generate a line of output for each telephone number that appears more than once in any form. Theline should give the telephone number in standard form, followed by a space, followed by the numberof times the telephone number appears in the directory. Arrange the output lines by telephone numberin ascending lexicographical order. If there are no duplicates in the input print the line:

No duplicates.

Print a blank line between datasets.

Sample Input 

1

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


题目大意:

给你组字符串,按照题目所给的规则解析成一组电话号码。

输出个数>=2的号码。


题目解析:

这题有个小技巧,就是先打表,用数组来实现字母和数字的转化,不然超时。

我原来是先转化成字符串再用atof()函数转化为数字,但是耗时422ms

后来看了题解直接通过计算转化为数字,速度快了很多。

还有就是因为统计号码,那一部分没写好WA了好多次。

最后,输出的时候要注意前补0

printf("%03d-%04d %d\n",tel[i]/10000,tel[i]%10000,cnt+1);
//这样输出

#include <stdio.h>
#include <algorithm>
#include <string>
#include <string.h>
#include <ctype.h>
using namespace std;
int map[128];
const int N = 100010;
int tel[N];
char arr[100];

void init() {
	map['A']=2;map['B']=2;map['C']=2;
	map['D']=3;map['E']=3;map['F']=3;
	map['G']=4;map['H']=4;map['I']=4;
	map['J']=5;map['K']=5;map['L']=5;
	map['M']=6;map['N']=6;map['O']=6;
	map['P']=7;map['R']=7;map['S']=7;
	map['T']=8;map['U']=8;map['V']=8;
	map['W']=9;map['X']=9;map['Y']=9;
}
/*
int change(char str[]) {
	int len = strlen(str);
	string temp;
	for(int i = 0;i < len; i++) {
		if(isalpha(str[i])) {
			str[i] = map[str[i]] + '0';
		}
		if(isalnum(str[i])) {
			temp += str[i];
		}
	}
	int num = atoi(temp.c_str());
	return num;
}
*/
int change(char str[]) {
	int num = 0;
	for(int i = 0;i < strlen(str); i++) {
		if(isdigit(str[i]))
			num = num*10+str[i]-'0';
		else if(isalpha(str[i]))
			num = num*10+map[str[i]];
	}
	return num;
}
int main() {
	int n;
	int t;
	init();
	scanf("%d",&t);
	while(t--) {
		int n;
		scanf("%d",&n);getchar();
		for(int i = 0; i < n; i++) {
			gets(arr);
			tel[i] = change(arr);
		}
		sort(tel,tel+n);

		int cnt=0,flag=0;
		int i,j;
		for(i=0; i < n; i=j) {
			for(j = i+1; j < n;j++) {
				if(tel[i]==tel[j])
					cnt++;
				else
					break;
			}
			if(cnt > 0) {
				if(flag==0)
					flag=1;
				printf("%03d-%04d %d\n",tel[i]/10000,tel[i]%10000,cnt+1);
			}
			cnt=0;
		}
		if(flag==0)
			puts("No duplicates.");
		if(t)
			putchar('\n');
	}
	return 0;
}

你可能感兴趣的:(uva,487--3279)