poj 1002 java_[Poj-1002]487-3279 Java解决

题目描述:

Time Limit: 2000MS

Memory Limit: 65536K

Total Submissions: 228365

Accepted: 39826

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

分析:

(1)问:如何处理‘-’字符,并将其在最后结果中显示?

答:首先对于处理字符串的字符方面,用到的是String中的CharAt()方法,对于读入字符串中的‘-’字符,不做处理或者说将空字符 添加到新建字符串后。最后输出中,用到的SubString()方法,截取0-3个字符放在‘-’之前,后续字符放在其后即可。

(2)问:对于字符串中的字母转化为数字,这方面该如何进行?

答:其实有两个办法,1是用Switch...Case语句来判断读入字符串的每个字符,并逐一转化为数字;2为利用ASCII码的联系,仍   然是对于字符逐一处理。两种比较笨的方法,适合我这种菜鸟。。

(3)问:字符串与其个数之间的映射该如何表示?

答:一开始根本没有用到这一步,只是纯粹的用字符串数组来存储这些读入的字符串(用StringBulider表示),并将其排序后比   较,输出最终的统计次数,但结果是——超时!   所以,第二次尝试时,使用到的是Map来表示String与Integer之间的映射, 在完成对于字符串的处理之后判断Map中是否存储了其,有则将值加一,这样我们最后输出值大于1的字符串即可。

参考代码:

这是我一开始超时的代码,但具体思想可以参考一下

1 importjava.util.Arrays;2 importjava.util.Scanner;3

4 public classPoj1002 {5

6 public static voidmain(String[] args) {7 //TODO Auto-generated method stub

8

9 Scanner in = newScanner(System.in);10 int num =in.nextInt();11

12 String[] sa = newString[num];13

14 for(int i=0; i

16 StringBuilder ts = newStringBuilder(in.next());17 StringBuilder ss = newStringBuilder();18

19 for(int j=0; j

21 char at =ts.charAt(j);22

23 //对于字符-不操作

24 if(at=='-'){25 continue;26 }27

28 int asc = at - 'A';29 if(asc<0){30 ss.append(at);31 }32 else if(asc<=2){33 ss.append(2);34 }35 else if(asc<=5){36 ss.append(3);37 }38 else if(asc<=8){39 ss.append(4);40 }41 else if(asc<=11){42 ss.append(5);43 }44 else if(asc<=14){45 ss.append(6);46 }47 else if(asc<=18 && asc!=16){//Q no mapping

48 ss.append(7);49 }50 else if(asc<=21){51 ss.append(8);52 }53 else if(asc<=24){//Z no mapping

54 ss.append(9);55 }56 }57

58 sa[i] = ss.toString();//将其几位sa数组中一员

59

60 }61

62 Arrays.sort(sa);//只有String才能使用,StringBuilder型则不能

63 boolean flag = false;64

65 for(int i=0; i

74 }75 if(count !=1){76 System.out.println(sa[i].substring(0, 3)+"-"+sa[i].substring(3, 7)+" "+count);77 flag = true;78 }79 }80

81 if(!flag){82 System.out.println("No duplicates.");83 }84

85

86 }87

88 }

正确通过的:

1 importjava.util.Iterator;2 importjava.util.Map;3 importjava.util.Scanner;4 importjava.util.Set;5 importjava.util.TreeMap;6

7 /*

8 * Second:Accepted9 */

10

11 public classPoj1002Sec {12

13 public static voidmain(String[] args) {14

15 Scanner sc = newScanner(System.in);16 int n =sc.nextInt();17

18 Map map = newTreeMap();19

20 for(int i=0; i

22 String jud = "";23 String rec =sc.nextLine();24

25 for(int j=0; j

31 char at =rec.charAt(j);32 switch(at){33 case 'A':34 case 'B':35 case 'C':36 jud += '2';37 break;38 case 'D':39 case 'E':40 case 'F':41 jud += '3';42 break;43 case 'G':44 case 'H':45 case 'I':46 jud += '4';47 break;48 case 'J':49 case 'K':50 case 'L':51 jud += '5';52 break;53 case 'M':54 case 'N':55 case 'O':56 jud += '6';57 break;58 case 'P':59 case 'R':60 case 'S':61 jud += '7';62 break;63 case 'T':64 case 'U':65 case 'V':66 jud += '8';67 break;68 case 'W':69 case 'X':70 case 'Y':71 jud += '9';72 break;73 default:74 break;75 }76 }77

78 if(jud.length()==7){79 String s = jud.substring(0, 3);80 jud = s + "-" + jud.substring(3);81 }82 else{83 break;84 }85

86 if(map.containsKey(jud)){87 int count = map.get(jud)+1;88 map.put(jud, count);89 }90 else{91 map.put(jud, 1);92 }93 }94

95 Set set =map.keySet();96 Iterator it =set.iterator();97 boolean flag = false;98 while(it.hasNext()){99 String s =it.next().toString();100 int count =map.get(s);101 if(count>1){102 flag = true;103 System.out.println(s+" "+count);104 }105 }106 if(!flag){107 System.out.println("No duplicates.");108 }109

110 }111 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值