码迷,mamicode.com
首页 > 编程语言 > 详细

POJ-1002-JAVA

时间:2016-09-24 21:34:33      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:

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

 思路比较简单:读取输入之后解析,统一为只含数字的格式,计算各个号码的出现次数,将重复出现的号码按字典序输出。

  解析的时候一开始用的是String的replaceAll方法,结果太慢超时了。后来改成多条if else判断。

  解析出来的结果因为是一对值(号码和次数),所以用HashTable存储的。这个地方在编译的时候遇到了一个问题,Java1.5(应该是java1.7一下都不支持) 不支持

Hashtable<String, Integer> record = new Hashtable<>();

这样的声明,需要在<>里加上String,Integer。

  还有一点就是java1.5的HashTable没有Replace()方法,Replace的功能可以由put()函数实现。

  比较花时间的是排序的部分,开始的办法是转换为ArrayList,用ArrayList.ArrayList<String>(Collection<? extends String> c)这个构造器,传入的参数为HashTable的keySet,然后用sort()方法,传入一个实现了Comparator接口的匿名内部类,进行排序。

1 ArrayList<String> sList=new ArrayList<>(record.keySet());
2         sList.sort(new Comparator<String>() {
3             @Override
4             public int compare(String o1, String o2) {
5                 return o1.compareTo(o2);
7             }
8             
9         });

但是java1.5下ArrayList没有sort方法,编译会出错。于是改成将ArrayList转换为字符串数组,结果速度特别慢。

  最后的办法是用了TreeSet,用HashTable的keySet构造一个TreeSet,元素按字母序递增。

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class P1002 {

    public static void main(String[] args) {
        Hashtable<String, Integer> record = new Hashtable<String,Integer>();
        Scanner in = new Scanner(System.in);
        int num = Integer.parseInt(in.nextLine());
        boolean hasDup=false;
        for (int i = 0; i < num; i++) {
            String phone=parse(in.nextLine());
            //这种方法太慢
            /*phone = phone.replaceAll("-", "");
            phone = phone.replaceAll("[ABC]", "2");
            phone = phone.replaceAll("[DEF]", "3");
            phone = phone.replaceAll("[GHI]", "4");
            phone = phone.replaceAll("[JKL]", "5");
            phone=phone.replaceAll("[MNO]", "6");
            phone=phone.replaceAll("[PRS]", "7");
            phone=phone.replaceAll("[TUV]", "8");
            phone=phone.replaceAll("[WXY]", "9");
            System.out.println(phone);*/
            /*if (record.containsKey(phone)) {
                int count = record.get(phone);
                hasDup = true;
                //record.replace(phone, Integer.valueOf(count), Integer.valueOf(count+1));
                record.put(phone, count+1);
            }else {
                record.put(phone, Integer.valueOf(1));
            }*/
            Integer count=record.get(phone);
            if(count!=null){
                hasDup=true;
            }
            record.put(phone, count==null?1:count+1);
        }
        if(!hasDup){
            System.out.println("No duplicates.");
            return;
        }
        /*转换为字符数组太慢
        String[] keys = new String[record.size()];
        new ArrayList<String>(record.keySet()).toArray(keys);
        for(int i=0;i<keys.length-1;i++){
            for(int j=keys.length-2;j>=i;j--){
                if (keys[j].compareTo(keys[j+1])>0) {
                    String tmp=keys[j];
                    keys[j]=keys[j+1];
                    keys[j+1]=tmp;
                }
            }
        }
        
        for(int i=0;i<keys.length;i++){
            String key=keys[i];
            if (record.get(key)>1) {
                System.out.println(key.substring(0, 3)+"-"+key.substring(3)+" "+record.get(key));
            }
        }
        */
        TreeSet<String> set = new TreeSet<String>(record.keySet());
        for(Iterator<String> iterator = set.iterator();iterator.hasNext();){
            String key=iterator.next();
            if (record.get(key)>1) {
                System.out.println(key.substring(0, 3)+"-"+key.substring(3)+" "+record.get(key));
            }

        }
    }
static String parse(String phone){ char[] phoneChars=new char[phone.length()]; int cnt=0; for(int i=0;i<phone.length();i++){ char c=phone.charAt(i); if(c==‘-‘) continue; if(c>=‘0‘ && c<=‘9‘){ phoneChars[cnt]=c; }else if (c>=‘A‘ && c<=‘C‘) { phoneChars[cnt]=‘2‘; }else if (c>=‘D‘ && c<=‘F‘) { phoneChars[cnt]=‘3‘; }else if (c>=‘G‘ && c<=‘I‘) { phoneChars[cnt]=‘4‘; }else if (c>=‘J‘ && c<=‘L‘) { phoneChars[cnt]=‘5‘; }else if (c>=‘M‘ && c<=‘O‘) { phoneChars[cnt]=‘6‘; }else if (c>=‘P‘ && c<=‘S‘) { phoneChars[cnt]=‘7‘; }else if (c>=‘T‘ && c<=‘V‘) { phoneChars[cnt]=‘8‘; }else if (c>=‘W‘ && c<=‘Y‘) { phoneChars[cnt]=‘9‘; } cnt++; } return new String(phoneChars, 0, cnt); } }

POJ-1002-JAVA

标签:

原文地址:http://www.cnblogs.com/tonyc/p/5903985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!