487-3279 |
Time limit: 3.000 seconds
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 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 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. There's a blank line between datasets.
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.
Print a blank line between datasets.
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
310-1010 2 487-3279 4 888-4567 3
Miguel A. Revilla
#include<stdio.h> #include<string.h> #include<ctype.h> #include<time.h> int telenum[100002]; int comp(const void *a,const void *b) { return *(int*)a-*(int*)b; } int main() { int i, j, n, T, cnt, len, flag, low, sum; char temp[100]; scanf("%d", &T); char alphalist[] = "2223334445556667777888999"; while(T--) { scanf("%d", &n); for(i=0; i<n; ++i) { scanf("%s", temp); len = strlen(temp); sum = 0; for(j=0 ; j<len; ++j) { if('0' <= temp[j] && '9' >= temp[j]) sum = sum*10 + (temp[j] - '0'); else if('A'<= temp[j] && 'Z' >= temp[j]) sum = sum*10 + (alphalist[temp[j]-'A']-'0'); } telenum[i] = sum; } qsort(telenum, n, sizeof(int),comp); low = cnt = flag = 0; for(i=0; i<n; ++i) { if(i+1 < n && telenum[low] == telenum[i+1]) cnt++; else { if(cnt) { flag = 1; printf("%03d-%04d %d\n", telenum[low]/10000, telenum[low]%10000, cnt+1); cnt = 0; } low = i+1; } } if(!flag) printf("No duplicates.\n"); if(T) printf("\n"); } return 0; }
解题思路:
这题如果说思路的话,看完题目就能知道需要得到的是什么,但常规的办法能AC就不会有那么低的通过率了,由于受到上一题的影响,这题我也是带着有点随便的心态去做,随便的心态的意思是说:没想过一次要AC
所以第一次按照常规的思路去实现:
输入字符串->转换成需要输出的形式即xxx-xxxx->查找已存储的字符串集中是否存在此串->存在的情况下在存储数量的相应的数组中num+1,不存在的情况下加入字符串集中->在存储串集中除去仅出现一次的字符串XXX-XXXX->根据字典顺序将剩下的排序后接着输出
提交状态:TLE
第二次改成:先将所有的字符串存储,然后按字典顺序排序, 再统计各个不同字符串出现的次数并且输出,而这些排序都是用冒泡,所以都在提交后出现超时
第三次改成:上网找了别人的源程序看了,确认思路没错后,那就知道了是排序超了时,冒泡的复杂度可是O(n^2) 所以改成严慧敏数据结构版的快速排序:
快排代码:
int select(int low, int high) { int i; i = telenum[low]; while(low < high) { for(; low < high && i <= telenum[high];) high--; telenum[low] = telenum[high]; for(; low < high && telenum[low] <= i;) low++; telenum[high] = telenum[low]; } telenum[low] = i; return low; } int prior(int low, int high) { int pivo; if(low < high) { pivo = select(low, high); prior(low, pivo-1); prior(pivo+1, high); } return 0; }
但还是超时了,再次看别人的代码,发现几乎都是用了这个函数 【qsort】
自己的代码加上这个函数后提交:WA!这时我就彻底的死心了!
再鼓捣了半个小时左右,终于发现了跟别人程序不同的地方,输出的时候没有用上 %03d-%04d 百度如下:
%3d--可以指定宽度,不足的左边补空格
%-3d--左对齐
%03d---一种左边补0 的等宽格式,
没理由的事情,加了0就判 accept