这道题目也比较简单,大致的意思就是对照翻译。。。最后需要注意,只有出现次数超过1次的电话号码才需要输出。。。
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class Main_1002_TransPhoneNumber {
public static void main(String[] args) throws IOException {
Scanner cin=new Scanner(System.in);
int length = cin.nextInt();
cin.nextLine();
Map<String,Integer> output = new TreeMap<String,Integer>();
for(int i=0;i<length;i++){
String s = cin.nextLine();
String ss = Trans(s);
if(output.containsKey(ss)){
Integer t = output.get(ss);
output.put(ss,t+1);
}else{
output.put(ss, 1);
}
}
if(output.size() == length){
System.out.println("No duplicates.");
}else{
Iterator iterator = output.keySet().iterator();
while(iterator.hasNext()){
String key = iterator.next().toString();
int value = output.get(key);
if(value > 1){
System.out.println(key+" "+output.get(key));
}
}
}
cin.close();
}
//时间证明map并不比if快些。。但是stringbuilder比string要快多了。。。
public static String Trans(String input){
StringBuilder output = new StringBuilder();
for(int i=0;i<input.length();i++){
char c = input.charAt(i);
if(output.length() == 3){
output.append("-");
}
if(c == '-'){
continue;
}
if(c == 'A' || c == 'B' || c == 'C'){
output.append("2");
continue;
}
if(c == 'D' || c == 'E' || c == 'F'){
output.append("3");
continue;
}
if(c == 'G' || c == 'H' || c == 'I'){
output.append("4");
continue;
}
if(c == 'J' || c == 'K' || c == 'L'){
output.append("5");
continue;
}
if(c == 'M' || c == 'N' || c == 'O'){
output.append("6");
continue;
}
if(c == 'P' || c == 'R' || c == 'S'){
output.append("7");
continue;
}
if(c == 'T' || c == 'U' || c == 'V'){
output.append("8");
continue;
}
if(c == 'W' || c == 'X' || c == 'Y'){
output.append("9");
continue;
}
output.append(c);
}
return output.toString();
}
}