Java Implementation of poj1002

Source: Internet
Author: User

Import java. io. BufferedInputStream;
Import java. io. DataInputStream;
Import java. io. IOException;
Import java. util. Iterator;
Import java. util. Map;
Import java. util. collections;
Import java. util. Set;
Import java. util. TreeMap;
Public class Main {
 
/**
* The following methods are used to complete the following functions:
* If it is a number, it will be returned directly. If it is only one character, it will return its corresponding number.
* @ Param c
* @ Return
*/
Public static char getNum (char c ){

// If the character is a number, it is returned directly.
If (Character. isDigit (c )){
Return c;
}

// If the character is A, B, or C, 2 is returned. The following rules are similar
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) throws IOException {

// DataInputStream (InputStream in). Use the specified underlying InputStream to create a DataInputStream.
DataInputStream scan = new DataInputStream (new BufferedInputStream (System. in ));

// TreeMap (). uses the natural sequence of keys to construct a new, empty tree ing.
Map <String, Integer> tm = new TreeMap ();

Int n = Integer. parseInt (scan. readLine (). trim ());
For (int I = 0; I <n; I ++ ){

// Remove "-" from the input number
String s = scan. readLine (). replace ("-","");

StringBuilder sb = new StringBuilder ();
For (int k = 0; k <s. length (); k ++ ){

// The resulting c may be a number ,#
Char c = getNum (s. charAt (k ));

// IsDigit (char ch). determines whether the specified character is a number.
/**
* The main logic of the if statement is:
* If c is a number, add it to the standard number string.
*/
If (Character. isDigit (c )){
Sb. append (c );
}
}

// Convert the string into a standard number
String result = sb. toString (). substring (0, 3) + '-' + sb. toString (). substring (3 );

// ContainsValue (Object value). If this ing maps one or more keys to the specified value, true is returned.
/**
* If · else mainly completes the following logic:
* 1) Determine whether the TreeMap contains this number. If yes, add the corresponding number of times to the number.
* 2) If no, set the number to 1.
*/
If (tm. containsKey (result )){

// Get (Object key). returns the value mapped to the specified key.
// If the ing does not contain any ing relationship for the key, null is returned.
Int count = tm. get (result) + 1;
Tm. put (result, count );
} Else {
// The TreeMap does not contain this number. Set this number to 1.
Tm. put (result, 1 );
}
}

// KeySet (). Return the Set view of the key contained in the ing.
Set se = tm. keySet ();
Iterator it = se. iterator ();
Boolean flag = false;
While (it. hasNext ()){
String s = it. next (). toString ();
Int count = tm. get (s );

/**
* The following if statements provide the following functions:
* If the number of occurrences is greater than 1, print the number and its occurrences.
*/
If (count> 1 ){
Flag = true;
System. out. println (s + "" + count );
}
}

// If no number in the standard format is repeated
// No duplicates is output.
If (! Flag ){
System. out. println ("No duplicates .");
}
}
}
 

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.