POJ1002
2014-04-12 20:31
246 查看
DescriptionBusinesses 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 ofthe 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 PizzaHut 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 2D, E, and F map to 3G, H, and I map to 4J, K, and L map to 5M, N, and O map to 6P, R, and S map to 7T, U, and V map to 8W, X, and Y map to 9There 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.InputThe 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 numberalone 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.OutputGenerate 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. Arrangethe 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-3279Sample Output
310-1010 2 487-3279 4 888-4567 3
其实就是一题处理字符串的题目,本来想使用正则表达式用数字替换字符的,结果超时了,想想也没错,这样多循环了8次。最后直接在网上找了一个函数替换掉,ac了,不过时间还是相当多。
import java.util.Map;import java.util.Scanner;import java.util.TreeMap;public class Main {public static char getNum(char c) {if (Character.isDigit(c)) {return c;}if (c == 'A' || c == 'B' || c == 'C') {return '2';}if (c == 'D' || c == 'E' || c == 'F') {return '3';}if (c == 'G' || c == 'H' || c == 'I') {return '4';}if (c == 'J' || c == 'K' || c == 'L') {return '5';}if (c == 'M' || c == 'N' || c == 'O') {return '6';}if (c == 'P' || c == 'R' || c == 'S') {return '7';}if (c == 'T' || c == 'U' || c == 'V') {return '8';}if (c == 'W' || c == 'X' || c == 'Y') {return '9';}return '#';}public static void main(String[] args) {// TODO Auto-generated method stubTreeMap<String, Integer> output = new TreeMap<String, Integer>();Scanner sc = new Scanner(System.in);int sum = sc.nextInt();int num = 0;boolean isDouble = true;while(num++ < sum){String str = sc.next();str = str.replaceAll("-", "");//删除横线/*str = str.replaceAll("[ABC]", "2");//正则表达式替换A或B或Cstr = str.replaceAll("[DEF]", "3");str = str.replaceAll("[GHI]", "4");str = str.replaceAll("[JKL]", "5");str = str.replaceAll("[MNO]", "6");str = str.replaceAll("[PRS]", "7");str = str.replaceAll("[TUV]", "8");str = str.replaceAll("[WXY]", "9");*/StringBuilder sb = new StringBuilder();for (int k = 0; k < str.length(); k++) {char c = getNum(str.charAt(k));if (Character.isDigit(c)) {sb.append(c);}}str = sb.toString();if(!output.containsKey(str)){output.put(str, 1);}else{isDouble = false;int count = output.get(str);count++;output.put(str, count);}//System.out.println(str);}if(isDouble){System.out.println("No duplicates. ");}else{for(Map.Entry<String, Integer> entry : output.entrySet()){if(entry.getValue() != 1){String outputStr = entry.getKey();outputStr = outputStr.substring(0, 3) + "-" + outputStr.substring(3, 7);System.out.println(outputStr + " " + entry.getValue());}}}//System.out.println(output);}}
相关文章推荐
- poj1002
- 北大poj ——487-3279(1002)
- poj1002 487-3279 解题报告
- POJ 1002
- POJ 1002 487-3279
- POJ 1002 487-3279 水题字符串
- POJ1002 487-3249
- POJ 1002 487-3279
- poj 1002 方便记忆的电话号码 字符串处理+排序
- POJ水题1002 487-3279
- POJ ACM 1002
- POJ-1002 487-3279
- POJ 1002 修订版
- poj1002
- poj 1002(487-3279)STL中的map
- POJ 1002 487-3279
- [POJ]1002 487-3279
- poj-1002
- poj 字符串相关之1002 487-3279
- POJ 1002 487-3279 (模拟 水题)