这OJ到底是怎样判定的,华为2016的第二题简单错误记录
输入描述:
一行或多行字符串。每行包括带路径文件名称,行号,以空格隔开。 文件路径为windows格式 如:E:\V1R2\product\fpgadrive.c 1325
输出描述:
将所有的记录统计并将结果输出,格式:文件名代码行数数目,一个空格隔开,如: fpgadrive.c 1325 1 结果根据数目从多到少排序,数目相同的情况下,按照输入第一次出现顺序排序。 如果超过8条记录,则只输出前8条记录. 如果文件名的长度超过16个字符,则只输出后16个字符
while (in.hasNext()) {String filename = in.next();
int num = in.nextInt();
String[] names = filename.split("\\\\");
String name = names[names.length - 1];
if (map.get(name + " " + num) == null) {
map.put(name + " " + num, 1);
} else {
map.put(name + " " + num, map.get(name + " " + num) + 1);
}
}
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Map.Entry;
public class Main {
static Scanner in = new Scanner(System.in);
static PrintStream out = new PrintStream(System.out);
public static void main(String[] args) {
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
StringBuilder sbr = new StringBuilder();
while (in.hasNext()) {
String filename = in.next();
int num = in.nextInt();
String[] names = filename.split("\\\\");
String name = names[names.length - 1];
if (map.get(name + " " + num) == null) {
map.put(name + " " + num, 1);
} else {
map.put(name + " " + num, map.get(name + " " + num) + 1);
}
}
Map<String, Integer> resultMap = sortMapByValue(map);
int count=0;
for (String s : resultMap.keySet()) {
count++;
String[] ss = s.split(" ");
String name = ss[0];
int line = Integer.valueOf(ss[1]);
if(name.length()>16){
sbr.append(name.substring(name.length()-16));
}else{
sbr.append(name);
}
sbr.append(" " + line+" "+resultMap.get(s) + "\n");
if(count==8){
break;
}
}
String sout=sbr.toString().substring(0,sbr.length()-1);
out.println(sout);
}
public static Map<String, Integer> sortMapByValue(Map<String, Integer> map) {
if (map == null || map.isEmpty()) {
return null;
}
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(
map.entrySet());
Collections.sort(entryList, new MapValueComparator());
Iterator<Map.Entry<String, Integer>> iter = entryList.iterator();
Map.Entry<String, Integer> tmpEntry = null;
while (iter.hasNext()) {
tmpEntry = iter.next();
sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
}
return sortedMap;
}
}
class MapValueComparator implements Comparator<Map.Entry<String, Integer>> {
public int compare(Entry<String, Integer> me1, Entry<String, Integer> me2) {
return me2.getValue().compareTo(me1.getValue());
}
}