| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
time exceed: Java source, Faint
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
String line = stdin.readLine();
int n = Integer.parseInt(line);
Voter vote = new Voter();
for (int i = 0; i < n; i++) {
String s = "" + stdin.readLine();
vote.vote(convert(s));
}
int count = 0;
Iterator i = vote.sort().iterator();
while (i.hasNext()) {
Map.Entry e = (Map.Entry) i.next();
String key = (String) e.getKey();
int value = ((Integer) e.getValue()).intValue();
if (value > 1) {
System.out.println(key+ " "+value);
count++;
}
}
if (count == 0) {
System.out.println("No duplicates.");
}
}
private static String convert(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
sb.append(convertChar(c));
}
String res = sb.substring(0, 3) + "-" + sb.substring(3);
return res;
}
/*
* 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
*/
static String[] map = { "ABC", "DEF", "GHI", "JKL", "MNO", "PRS", "TUV",
"WXY" };
private static String convertChar(char c) {
if ("0123456789".indexOf(c) >= 0) {
return "" + c;
}
for (int i = 0; i < map.length; i++) {
String s = map[i];
if (s.indexOf(c) >= 0) {
return "" + (i + 2);
}
}
return "";
}
static class Voter {
Map map = new HashMap();
public Voter() {
}
private final static Integer ONE = new Integer(1);
public void vote(Object o) {
try {
Object v = map.get(o);
if (v != null) {
map.put(o, new Integer(((Integer) v).intValue() + 1));
} else {
map.put(o, ONE);
}
} catch (Throwable t) {
}
}
/**
* @return list of Map.Entry
*/
public List sort() {
List res = new ArrayList(map.entrySet());
Collections.sort(res, new StringKeyEntryComparator());
return res;
}
}
static class StringKeyEntryComparator implements Comparator {
/*
* (non-Javadoc)
*
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(Object o1, Object o2) {
return ((String) ((Map.Entry) o1).getKey())
.compareTo((String) ((Map.Entry) o2).getKey());
}
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator