import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class poj1002 {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
HashMap<String, Integer> map = new HashMap<String, Integer>();
int loop = Integer.parseInt(in.readLine().trim());
for (int i = 0; i < loop; i++) {
String tmp = in.readLine().trim().replaceAll("-", "");
tmp = parse(tmp);
// tmp = tmp.replaceAll("[A-C]", "2");
// tmp = tmp.replaceAll("[D-F]", "3");
// tmp = tmp.replaceAll("[G-I]", "4");
// tmp = tmp.replaceAll("[J-L]", "5");
// tmp = tmp.replaceAll("[M-O]", "6");
// tmp = tmp.replaceAll("[PRS]", "7");
// tmp = tmp.replaceAll("[TUV]", "8");
// tmp = tmp.replaceAll("[WXY]", "9");
String head = tmp.substring(0, 3) + "-"
+ tmp.substring(3, tmp.length());
Integer times = map.get(head);
map.put(head, times == null ? 1 : times + 1);
}
Set<String> keys = new TreeSet<String>(map.keySet());
boolean hasOutput = false;
for (Iterator<String> it = keys.iterator(); it.hasNext();) {
String key = it.next();
if (map.get(key) > 1) {
hasOutput = true;
System.out.println(key + " " + map.get(key));
}
}
if (!hasOutput) {
System.out.println("No duplicates.");
}
}
private static String parse(String tmp) {
String result = "";
int length = tmp.length();
for (int i = 0; i < length; i++) {
char ch = tmp.charAt(i);
if (ch >= '0' && ch <= '9') {
result += ch;
} else {
result += convert(ch);
}
}
return result;
}
private static int convert(char ch) {
if (ch >= 'A' && ch <= 'C') {
return 2;
} else if (ch >= 'D' && ch <= 'F') {
return 3;
} else if (ch >= 'G' && ch <= 'I') {
return 4;
} else if (ch >= 'J' && ch <= 'L') {
return 5;
} else if (ch >= 'M' && ch <= 'O') {
return 6;
} else if (ch >= 'P' && ch <= 'S' && ch != 'Q') {
return 7;
} else if (ch >= 'T' && ch <= 'V') {
return 8;
} else if (ch >= 'W' && ch <= 'Y') {
return 9;
} else {
return 0;
}
}
}
修改后代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class poj1002 {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
HashMap<String, Integer> map = new HashMap<String, Integer>();
int loop = Integer.parseInt(in.readLine().trim());
for (int i = 0; i < loop; i++) {
String tmp = in.readLine().trim().replaceAll("-", "");
tmp = parse(tmp);
// tmp = tmp.replaceAll("[A-C]", "2");
// tmp = tmp.replaceAll("[D-F]", "3");
// tmp = tmp.replaceAll("[G-I]", "4");
// tmp = tmp.replaceAll("[J-L]", "5");
// tmp = tmp.replaceAll("[M-O]", "6");
// tmp = tmp.replaceAll("[PRS]", "7");
// tmp = tmp.replaceAll("[TUV]", "8");
// tmp = tmp.replaceAll("[WXY]", "9");
// String head = tmp.substring(0, 3) + "-"
// + tmp.substring(3, tmp.length());
Integer times = map.get(tmp);
map.put(tmp, times == null ? 1 : times + 1);
}
Set<String> keys = new TreeSet<String>(map.keySet());
boolean hasOutput = false;
for (Iterator<String> it = keys.iterator(); it.hasNext();) {
String key = it.next();
if (map.get(key) > 1) {
hasOutput = true;
System.out.println(key.substring(0, 3) + "-"
+ key.substring(3, key.length()) + " " + map.get(key));
}
}
if (!hasOutput) {
System.out.println("No duplicates.");
}
}
private static String parse(String tmp) {
String result = "";
int length = tmp.length();
for (int i = 0; i < length; i++) {
char ch = tmp.charAt(i);
if (ch >= '0' && ch <= '9') {
result += ch;
} else {
result += convert(ch);
}
}
return result;
}
private static int convert(char ch) {
// if (ch >= 'A' && ch <= 'C') {
// return 2;
// } else if (ch >= 'D' && ch <= 'F') {
// return 3;
// } else if (ch >= 'G' && ch <= 'I') {
// return 4;
// } else if (ch >= 'J' && ch <= 'L') {
// return 5;
// } else if (ch >= 'M' && ch <= 'O') {
// return 6;
// } else if (ch >= 'P' && ch <= 'S' && ch != 'Q') {
// return 7;
// } else if (ch >= 'T' && ch <= 'V') {
// return 8;
// } else if (ch >= 'W' && ch <= 'Y') {
// return 9;
// } else {
// return 0;
// }
if(ch=='Q'||ch=='Z'){
return 0;
}
if(ch>'Q'){
ch -= 1;
}
return (ch - 'A')/3+2;
}
}