Poj-1002-java

Source: Internet
Author: User

Descriptionbusinesses like to has memorable telephone numbers. One-to-make a telephone number memorable are to has 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 are 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 a telephone number memorable is to group the digits in a memorable the. 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 are seven decimal digits with a hyphen between the third and fourth digits (e.g. 88 8-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 was not dialed, and can be added and removed as necessary. The standard form of Tut-glop was 888-4567, the standard form of 310-gino was 310-4466, and the standard form of 3-10-10-10 Is 310-1010.

Telephone numbers is equivalent if they has the same standard form. (They dial the same number.)

Your Company was compiling a directory of telephone numbers from local businesses. As part of the quality control process, you want to check, that no, or more, businesses in the directory has the same t Elephone number.

Input

The input would 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 integ Er 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 would be digits or letters.

Output

Generate a line of output for each telephone number this appears more than once in any form. The line should give the telephone number in standard form, followed by a space, and followed by the number of times the TELEP Hone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order. If there is no duplicates in the input print of the line:

No duplicates.

Sample Input

124873279its-easy888-45673-10-10-10888-gloptut-glop967-11-11310-ginof101010888-1200-4-8-7-3-2-7-9-487-3279

Sample Output

310-1010 2487-3279 4888-4567 3

The idea is simple: After reading the input parsing, unified for only the number of formats, calculate the number of occurrences, the number of repeated occurrences of the dictionary output.

Parsing is the beginning of a string ReplaceAll method, the result is too slow timeout. It is later changed to multiple if else judgments.

The parsed result is a pair of values (number and number of times), so it is stored with Hashtable. This place in the compile time encountered a problem, Java1.5 (should be java1.7 not supported) does not support

New Hashtable<> ();

Such a statement, need to add String,integer in <>.

Another thing is that java1.5 's Hashtable without the Replace () method, the Replace function can be implemented by the put () function.

The comparison takes time to sort the parts, the starting method is to convert to ArrayList, with arraylist.arraylist<string> (collection<? extends string> c) This constructor, The passed-in parameter is Hashtable keyset, and the sort () method is used to pass in an anonymous inner class that implements the comparator interface.

1Arraylist<string> slist=NewArraylist<>(Record.keyset ());2Slist.sort (NewComparator<string>() {3 @Override4              Public intCompare (String O1, String O2) {5                   returnO1.compareto (O2);7             }8             9});

However, java1.5 ArrayList does not have the Sort method, the compilation will be wrong. The result is changed to convert the ArrayList to a string array, which results in a particularly slow speed.

The last way is to use the TreeSet, with Hashtable keyset constructs a treeset, the elements in alphabetical order increment.

Importjava.util.ArrayList;ImportJava.util.Comparator;Importjava.util.Hashtable;ImportJava.util.Iterator;ImportJava.util.Scanner;ImportJava.util.Set;ImportJava.util.TreeSet; Public classP1002 { Public Static voidMain (string[] args) {Hashtable<string, integer> record =NewHashtable<string,integer>(); Scanner in=NewScanner (system.in); intnum =Integer.parseint (In.nextline ()); Booleanhasdup=false;  for(inti = 0; i < num; i++) {String phone=Parse (In.nextline ()); //This method is too slow            /*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; }        /*convert to character array too slow 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 =NewTreeset<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)); }        }    }
Staticstring Parse (string phone) {Char[] phonechars=New Char[Phone.length ()]; intCnt=0; for(intI=0;i<phone.length (); i++){ CharC=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 NewString (phonechars, 0, CNT); }}

Poj-1002-java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.