我用的map建立的电话号码和重复次数的关系 用一个字串 "ABCDEFGHIJKLMNOPRSTUVWXY" 每个字母的index/3 +2的值就是其所代表的电话号码 。。TLE了三次了
先把原题和没修改好的代码贴上 样例过了。。虽然说样例本身就很水。。。。
487-3279 |
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
1 #include<stdio.h> 2 #include<iostream> 3 #include<string> 4 #include<algorithm> 5 #include<map> 6 #include<cctype> 7 8 using namespace std; 9 string refer = "ABCDEFGHIJKLMNOPRSTUVWXY"; 10 11 int main(void) 12 { 13 //freopen("755.out", "w", stdout); 14 int cases; 15 int lines; 16 map<string, int> numlst; 17 scanf("%d", &cases); 18 for (int oo = 0; oo < cases; oo++) 19 { 20 numlst.clear(); 21 scanf("%d", &lines); 22 string telnum; 23 string telNUM; 24 //getchar(); 25 for (int i = 0; i < lines; i++) 26 { 27 telNUM.clear(); 28 cin >> telnum; 29 int size1 = telnum.size(); 30 for (int j = 0; j <size1 ; j++) 31 { 32 if (telnum[j] == ‘-‘) continue; 33 else if (isalpha(telnum[j])) 34 { 35 for (int cnt = 0; cnt < 24;cnt++) 36 if (telnum[j] == refer[cnt]) { telNUM += (char)((cnt / 3) + 2+‘0‘); break; } 37 } 38 else telNUM += telnum[j]; 39 } 40 if (!numlst.count(telNUM)) 41 numlst.insert(make_pair(telNUM, 1)); 42 else 43 { 44 for (map<string, int>::iterator ii = numlst.begin(); ii != numlst.end(); ii++) 45 { 46 if ((*ii).first == telNUM) { (*ii).second++; break; } 47 } 48 } 49 } 50 int ok = 0; 51 for (map<string, int>::iterator ii = numlst.begin(); ii != numlst.end(); ii++) 52 { 53 if ((*ii).second != 1) 54 { 55 ok = 1; 56 for (int j = 0; j < 8; j++) 57 { 58 if (j == 3) printf("-"); 59 else printf("%c", (*ii).first[j]); 60 } 61 printf(" %d\n", (*ii).second); 62 //printf("\n"); 63 } 64 } 65 if (!ok) printf("No duplicates.\n"); 66 printf("\n"); 67 } 68 //while (1) getchar(); 69 return 0; 70 }
最近心不能静下来,竟然连输出和样例输出不一样都没看出来 ,在处理那个应该加 “-” 的电话号码时出了问题导致只输出6位数字,而且忘记判断没有重复的号码这个情况。。
这种状态可不行 ,要静下来仔细做
这道题的通过情况不好。。
总结:
PRE的代码用的count判断是否存在 ,然后又用迭代器一个个线性找,时间复杂度大大上升。
NOW的代码直接用find返回一个迭代器 find用的搜索算法比自己写的最简单的快很多。时间复杂度不至于过高
PRE的代码忘记在每组输出后输出回车
PRE的代码没判断所有电话号码均不重复的情况
PRE的代码在输出电话号码时 由于方法错误 导致少输出一位 而NOW AC的代码这个地方改的复杂
以下是AC的代码
1 //#define LOCAL 2 #include<stdio.h> 3 #include<iostream> 4 #include<string> 5 #include<algorithm> 6 #include<map> 7 #include<cctype> 8 9 using namespace std; 10 string refer = "ABCDEFGHIJKLMNOPRSTUVWXY"; 11 12 int main(void) 13 { 14 #ifdef LOCAL 15 freopen("755.out", "w", stdout); 16 #endif 17 int cases; 18 int lines; 19 map<string, int> numlst; 20 scanf("%d", &cases); 21 for (int oo = 0; oo < cases; oo++) 22 { 23 numlst.clear(); 24 scanf("%d", &lines); 25 string telnum; 26 string telNUM; 27 //getchar(); 28 for (int i = 0; i < lines; i++) 29 { 30 telNUM.clear(); 31 cin >> telnum; 32 int size1 = telnum.size(); 33 for (int j = 0; j <size1 ; j++) 34 { 35 if (telnum[j] == ‘-‘) continue; 36 else if (isalpha(telnum[j])) 37 { 38 for (int cnt = 0; cnt < 24;cnt++) 39 if (telnum[j] == refer[cnt]) { telNUM += (char)((cnt / 3) + 2+‘0‘); break; } 40 } 41 else telNUM += telnum[j]; 42 } 43 map<string, int>::iterator fndit=numlst.find(telNUM); 44 if (fndit==numlst.end()) 45 numlst.insert(make_pair(telNUM, 1)); 46 else 47 { 48 (*fndit).second++; 49 } 50 } 51 int ok = 0; 52 for (map<string, int>::iterator ii = numlst.begin(); ii != numlst.end(); ii++) 53 { 54 if ((*ii).second != 1) 55 { 56 ok = 1; 57 bool have = 0; 58 for (int j = 0; j < 7; j++) 59 { 60 if (j == 3) printf("-"); 61 printf("%c", (*ii).first[j]); 62 } 63 printf(" %d\n", (*ii).second); 64 //printf("\n"); 65 } 66 } 67 if (!ok) printf("No duplicates.\n"); 68 if(oo<cases-1) printf("\n"); 69 } 70 //while (1) getchar(); 71 return 0; 72 }
UVA 755 487-3279,布布扣,bubuko.com
原文:http://www.cnblogs.com/VOID-133/p/3571909.html