本题将会给出
条报错信息,确切数字未知,您需要一直读入直到文件结尾;您也可以参考 牛客网在线判题系统使用帮助 获得更多的使用帮助。每条报错信息描述如下:
在一行上先输入一个长度为
的字符串
代表文件路径;随后,在同一行输入一个整数
代表行号。
文件路径的格式如题干所述,保证文件名不为空。
至多八行,每行先输出一个长度为
的字符串
,代表文件名;随后,在同一行输出错误行号、报错次数。
D:\oblemsinnowcoder 12 D:\nowcoderproblemsinnowcoder 12 D:\nowcoder\problemsinnowcoder 13 D:\oj\problemsinnowcoder 13
oblemsinnowcoder 12 2 oblemsinnowcoder 13 2
在这个样例中,这四条报错信息去除文件路径后,由于文件名长度均超过
个字符,故我们只保留最后
个字符,得到的文件名均为
。所以,我们将它们看作同一个文件,按照报错行号划分即可。
A:\aa 1 B:\b 1 C:\c 1 D:\d 1 E:\e 1 F:\f 1 G:\g 1 H:\h 1 I:\i 1 A:\aa 1
b 1 1 c 1 1 d 1 1 e 1 1 f 1 1 g 1 1 h 1 1 i 1 1
在这个样例中,第一、十条报错信息完全相同,但是我们以其第一次出现的顺序为准,在输出最后
条记录时,不包含这一报错。
D:\zwtymj\xccb\ljj\cqzlyaszjvlsjmkwoqijggmybr 645 E:\je\rzuwnjvnuz 633 C:\km\tgjwpb\gy\atl 637 F:\weioj\hadd\connsh\rwyfvzsopsuiqjnr 647 E:\ns\mfwj\wqkoki\eez 648 D:\cfmwafhhgeyawnool 649 E:\czt\opwip\osnll\c 637 G:\nt\f 633 F:\fop\ywzqaop 631 F:\yay\jc\ywzqaop 631 D:\zwtymj\xccb\ljj\cqzlyaszjvlsjmkwoqijggmybr 645
rzuwnjvnuz 633 1 atl 637 1 rwyfvzsopsuiqjnr 647 1 eez 648 1 fmwafhhgeyawnool 649 1 c 637 1 f 633 1 ywzqaop 631 2
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); List<String> errList = new ArrayList<>(); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String inputStr = in.nextLine().trim();//去除空行 //空行则返回 if (inputStr .isEmpty()) return; if (inputStr.equals("END")) { //检测到输入“END”时,退出循环 break; } String[] arr1 = inputStr.split(" "); //长度不为2表示输入有问题,退出 if (arr1.length != 2) return; //处理文件路径 String str1 = arr1[0]; if (str1.length() < 3 ) return; char c1 = str1.charAt(0); char c2 = str1.charAt(1); char c3 = str1.charAt(2); if (c1 < 'A' || c1 > 'Z' || c2 != ':' || c3 != '\\') {//非正常格式,退出 return; } String[] addressArr = str1.split("\\\\"); if (addressArr.length < 2 ) {//非正常格式,退出 return; } String address; String addressTemp = addressArr[addressArr.length - 1]; if (addressTemp.length() > 16) { //截取地址的后16位 address = addressTemp.substring(addressTemp.length() - 16, addressTemp.length()); } else { address = addressTemp; } //处理行数 String str2 = arr1[1]; //为空退出 if (str2.length() < 0) return; //不是数字组成,退出 for (char c : str2.toCharArray()) { if (!Character.isDigit(c)) { return; } } //获取行号 String line = str2.trim(); List<String> tempList = new ArrayList<>(); String errRecord = address + " " + line; errList = addRecord(errRecord, errList); } //如果errList的长度>8 ,则从倒数第8个开始输出 int i = 0; if (errList.size() > 8 ) { i = errList.size() - 8; } for (; i < errList.size(); i ++) { System.out.println(errList.get(i)); } in.close(); } //对于重复的文件名和行数,++ public static List<String> addRecord(String str, List<String> list) { String[] arr = str.split(" "); if (list.size() == 0) { //为空时,直接加入这行,并且计数为1 str = str + " 1"; list.add(str); return list; } for (int i = 0; i < list.size(); i ++) { String[] arr1 = list.get(i).split(" "); //取出list中的计数部分,转化为数字 String count = arr1[2]; int n = Integer.parseInt(count); //将输入的 字符串分割成可以判断的数组,进行对比判断 String[] arr2 = str.split(" "); if ( arr1[0].equals(arr2[0]) && arr1[1].equals(arr2[1])) { //对比address和line是否已经存在,如果存在,则在计数上+1 ,并且替换原有list位置中的数据 n++; str = str + " " + n + ""; list.set(i, str); return list; } else { if (i == list.size() - 1) { //不存在则直接+1 str = str + " 1"; list.add(str); return list; } } } return list; } }
import java.util.Scanner; import java.util.HashMap; import java.util.ArrayList; public class Main { public static void main(String[] args) { HashMap<String,Integer> map=new HashMap<>(); ArrayList<String[]> list=new ArrayList<>(); Scanner in = new Scanner(System.in); while (in.hasNext()) { String[] str=in.nextLine().split(" "); String s=str[0].split("\\\\")[str[0].split("\\\\").length-1]; if(s.length()>16){ s=s.substring(s.length()-16,s.length()); } if(map.containsKey(s+str[1])){ map.put(s+str[1],map.get(s+str[1])+1); }else{ map.put(s+str[1],1); String[] s1=new String[2]; s1[0]=s; s1[1]=str[1]; list.add(s1); } } int i=0; if(list.size()>8){ i=list.size()-8; } for(;i<list.size();i++){ System.out.println(list.get(i)[0]+" "+list.get(i)[1]+" "+map.get(list.get(i)[0]+list.get(i)[1])); } in.close(); } }
import java.util.HashMap; import java.util.LinkedList; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); LinkedList<String> names = new LinkedList<>(); //保存顺序 HashMap<String, Integer> map = new HashMap<>(); //保存次数 // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String s = in.next(); int col = in.nextInt(); String n = getName(s) + " " + col; // System.out.println(n+"--------------------"); if (!map.containsKey(n)) { names.add(n); map.put(n, 1); } else { col = map.get(n) + 1; map.put(n, col); } } if (names.size() <= 8) { for (String name : names) { int num = map.get(name); System.out.println(name + " " + num); } }else{ for (int i = names.size()-8; i < names.size() ; i++) { System.out.println(names.get(i)+" "+map.get(names.get(i))); } } } public static String getName(String file) { String[] split = file.split("\\\\"); String name = split[split.length - 1]; if (name.length() <= 16) return name; return name.substring(name.length() - 16); } }
class ErrorRecordNode { String fileNameAndLineNumber; // 文件名和行号 int count; // 错误次数 ErrorRecordNode next; // 下一个节点 // 构造方法 public ErrorRecordNode(String fileNameAndLineNumber, int count) { this.fileNameAndLineNumber = fileNameAndLineNumber; this.count = count; this.next = null; // 默认指向null } // 获取链表节点个数 public static int getNodeCount(ErrorRecordNode head) { int count = 0; // 节点计数器 ErrorRecordNode current = head; // 当前节点指针 while (current != null) { count++; // 计数加一 current = current.next; // 移动到下一个节点 } return count; } // 尾插法 public static ErrorRecordNode appendTail(ErrorRecordNode head, String fileNameAndLineNumber, int count) { ErrorRecordNode newNode = new ErrorRecordNode(fileNameAndLineNumber, count); if (head == null) { // 如果链表为空,则新节点直接作为头节点返回 return newNode; } ErrorRecordNode current = head; while (current.next != null) { current = current.next; } current.next = newNode; // 将新节点添加到链表尾部 return head; } // 去除头节点 public static ErrorRecordNode removeHead(ErrorRecordNode head) { if (head == null) { return null; // 链表为空,直接返回null } return head.next; // 返回原头节点的下一节点 } // 更新 count 方法 public static void updateCount(ErrorRecordNode head, String fileNameAndLineNumber) { ErrorRecordNode current = head; while (current != null) { if (current.fileNameAndLineNumber.equals(fileNameAndLineNumber)) { current.count++; // 找到匹配节点,count加一 return; // 更新后返回,不再遍历剩余节点 } current = current.next; // 否则继续遍历下一个节点 } System.out.println("未找到匹配的节点: " + fileNameAndLineNumber); } // 覆盖 toString 方法,用于链表的可视化 @Override public String toString() { StringBuilder sb = new StringBuilder(); ErrorRecordNode current = this; while (current != null) { sb.append("{") .append("fileNameAndLineNumber='").append(current.fileNameAndLineNumber).append("'") .append(", count=").append(current.count) .append("} -> "); current = current.next; } sb.append("null"); // 表示链表的结尾 return sb.toString(); } // 测试用例 public static void main(String[] args) { // 创建链表头节点 ErrorRecordNode head = new ErrorRecordNode("file1.cpp:10", 1); // 尾插几个节点 head = appendTail(head, "file2.cpp:20", 2); head = appendTail(head, "file3.cpp:30", 3); System.out.println("链表内容:"); System.out.println(head); // 去除头节点 head = removeHead(head); System.out.println("去除头节点后:"); System.out.println(head); // 再次去除头节点 head = removeHead(head); System.out.println("再次去除头节点后:"); System.out.println(head); } }
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int location = 1; HashMap<String, Integer> firstLocationMap = new HashMap<>(); ErrorRecordNode head = null; while (in.hasNextLine()) { String s = in.nextLine(); if (s.equals("end")) { break; } String[] split = s.split("\\\\"); String fileNameAndLineNumber = split[split.length - 1]; String[] fileNameAndLineNumberArr = fileNameAndLineNumber.split(" "); String fileName = fileNameAndLineNumberArr[0]; if (fileName.length() > 16) { //文件名大于16位的保留后16位 fileName = fileName.substring(fileName.length() - 16); fileNameAndLineNumber= fileName + " " + fileNameAndLineNumberArr[1]; } //检查这个文件和行数 首次出现的位置 //如果没有key 表示第一次出现 直接加入 if (!firstLocationMap.containsKey(fileNameAndLineNumber)) { firstLocationMap.put(fileNameAndLineNumber, location); //如果是第一次加入,一定会存入结果,且次数是1 //但是还要考虑链表是不是已经满了 , 满8个就去掉头节点 if (ErrorRecordNode.getNodeCount(head) == 8) { head = ErrorRecordNode.removeHead(head); } //第一次加入,一定会存入结果,且次数是1 head = ErrorRecordNode.appendTail(head, fileNameAndLineNumber, 1); } else { //如果这个文件和行数已经出现过,那么检查一下首次位置在哪里 int firstLocation = firstLocationMap.get(fileNameAndLineNumber); //如果当前位置 - 首次出现位置 大于 7 说明这个错误已经过于久远了不能存入结果 比如 当前位置是9 首次出现位置是1 相减得到8 1到9是九个错误了 第一个错误如果再犯就不能再加入了 //所以如果小于等于7 那么说明这个错误还不是很久远 那么就加入结果 更新其count if (location - firstLocation<=7) { ErrorRecordNode.updateCount(head, fileNameAndLineNumber); } } location++; } while (head != null) { System.out.println(head.fileNameAndLineNumber + " " + head.count); head = head.next; } } } class ErrorRecordNode { String fileNameAndLineNumber; // 文件名和行号 int count; // 错误次数 ErrorRecordNode next; // 下一个节点 // 构造方法 public ErrorRecordNode(String fileNameAndLineNumber, int count) { this.fileNameAndLineNumber = fileNameAndLineNumber; this.count = count; this.next = null; // 默认指向null } // 获取链表节点个数 public static int getNodeCount(ErrorRecordNode head) { int count = 0; // 节点计数器 ErrorRecordNode current = head; // 当前节点指针 while (current != null) { count++; // 计数加一 current = current.next; // 移动到下一个节点 } return count; } // 尾插法 public static ErrorRecordNode appendTail(ErrorRecordNode head, String fileNameAndLineNumber, int count) { ErrorRecordNode newNode = new ErrorRecordNode(fileNameAndLineNumber, count); if (head == null) { // 如果链表为空,则新节点直接作为头节点返回 return newNode; } ErrorRecordNode current = head; while (current.next != null) { current = current.next; } current.next = newNode; // 将新节点添加到链表尾部 return head; } // 去除头节点 public static ErrorRecordNode removeHead(ErrorRecordNode head) { if (head == null) { return null; // 链表为空,直接返回null } return head.next; // 返回原头节点的下一节点 } // 更新 count 方法 public static void updateCount(ErrorRecordNode head, String fileNameAndLineNumber) { ErrorRecordNode current = head; while (current != null) { if (current.fileNameAndLineNumber.equals(fileNameAndLineNumber)) { current.count++; // 找到匹配节点,count加一 return; // 更新后返回,不再遍历剩余节点 } current = current.next; // 否则继续遍历下一个节点 } System.out.println("未找到匹配的节点: " + fileNameAndLineNumber); } // 覆盖 toString 方法,用于链表的可视化 @Override public String toString() { StringBuilder sb = new StringBuilder(); ErrorRecordNode current = this; while (current != null) { sb.append("{") .append("fileNameAndLineNumber='").append(current.fileNameAndLineNumber).append("'") .append(", count=").append(current.count) .append("} -> "); current = current.next; } sb.append("null"); // 表示链表的结尾 return sb.toString(); } // 测试用例 public static void main(String[] args) { // 创建链表头节点 ErrorRecordNode head = new ErrorRecordNode("file1.cpp:10", 1); // 尾插几个节点 head = appendTail(head, "file2.cpp:20", 2); head = appendTail(head, "file3.cpp:30", 3); System.out.println("链表内容:"); System.out.println(head); // 去除头节点 head = removeHead(head); System.out.println("去除头节点后:"); System.out.println(head); // 再次去除头节点 head = removeHead(head); System.out.println("再次去除头节点后:"); System.out.println(head); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Map<String, Integer> map = new LinkedHashMap<>(); while (sc.hasNext()) { String s = sc.next(); int line = sc.nextInt(); String[] strings = s.split("\\\\"); String fileName = strings[strings.length - 1]; if (fileName.length() > 16) { fileName = fileName.substring(fileName.length() - 16); } fileName = fileName + " " + line; map.put(fileName, map.getOrDefault(fileName, 0) + 1); } int i = 0; for (String key : map.keySet()) { if (i >= map.size() - 8) { System.out.println(key + " " + map.get(key)); } i++; } sc.close(); } }
import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); LinkedHashSet<String> myHashSet = new LinkedHashSet<String>(); HashMap<String, Integer> myHashMap = new HashMap<String, Integer>(); int before, after; // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String line = in.nextLine().toString(); if (line == "") { continue; } String errAndNum = line.substring(line.lastIndexOf('\\') + 1); String[] sufAndNum = errAndNum.split(" "); String err = sufAndNum[0].substring(Math.max(0, sufAndNum[0].length() - 16)); String num = sufAndNum[1]; err = err + " " + num; before = myHashSet.size(); myHashSet.add(err); after = myHashSet.size(); if (before + 1 == after) { myHashMap.put(err, 1); } else if (before == after) { myHashMap.put(err, myHashMap.get(err) + 1); } else { System.out.println("ERROR during count duplicate times"); System.exit(1); } } in.close(); ArrayList<String> myArrList = new ArrayList<String>(myHashSet); int max = myArrList.size(); int begin = Math.max(0, max - 8); for (int i = begin; i < max; i++) { System.out.println(myArrList.get(i) + " " + myHashMap.get(myArrList.get(i))); } } }
import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.concurrent.CopyOnWriteArrayList; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { static Map<String ,String[] > errorMap = new HashMap<>(); static List<String> list = new CopyOnWriteArrayList<>(); public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 List<String> strError = new CopyOnWriteArrayList<>(); while (in.hasNext()) { // 注意 while 处理多个 case strError.add(in.nextLine()); } saveErrorImg(strError); int length = list.size()>8?list.size()-8:0; for(int i = length ; i < list.size() ; i++){ System.out.println(list.get(i).substring(0,list.get(i).length() - errorMap.get(list.get(i))[0].length()) + " " + errorMap.get(list.get(i))[0] +" " + errorMap.get(list.get(i))[1]); } } private static void saveErrorImg(List<String> strError){ for(String error : strError){ String[] sError = error.split(" "); String s1= sError[0].substring(sError[0].lastIndexOf("\\")+1); String s2= s1.substring(s1.length()>16?s1.length()-16:0); String s3= s2+sError[1]; if(errorMap.containsKey(s3)){ errorMap.get(s3)[1] = Integer.parseInt(errorMap.get(s3)[1]) +1 +""; }else{ errorMap.put(s3,new String[]{sError[1],"1"}); list.add(s3); } } } }
import java.util.ArrayList; import java.util.HashMap; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); HashMap<String, Integer> map = new HashMap<>(); ArrayList<String> keyOrder = new ArrayList<>(); while (in.hasNextLine()) { // 注意 while 处理多个 case String[] input = in.nextLine().split(" "); String[] arrKey = input[0].split("\\\\"); String key = arrKey[arrKey.length-1]; if (key.length()>16) { key = key.substring(key.length()-16); } key = key + " " + input[1]; if (map.get(key)==null) { map.put(key, 1); keyOrder.add(key); } else { map.put(key, map.get(key)+1); } } int i = 0; if (keyOrder.size()>8) { i = keyOrder.size()-8; } for (; i<keyOrder.size(); i++) { System.out.println(keyOrder.get(i)+" "+map.get(keyOrder.get(i))); } } }
public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] result = new String[8]; HashMap<String, Integer> map = new HashMap<>(); int length = 0; while (in.hasNextLine()) { String str = in.nextLine(); String strs[] = str.split(" "); String[] path = strs[0].split("\\\\"); String name = path[path.length - 1]; name = name.length() > 16 ? name.substring(name.length() - 16) : name; String s = name + " " + strs[1]; if (map.get(s) != null) { map.merge(s, 1, Integer::sum); }else { map.put(s, 1); result[length % 8] = s; length++; } } // 打印 int startIndex = length > 8 ? length % 8 : 0; length = Math.min(length, 8); for (int i = 0; i < length; i++) { String resultLine = result[(startIndex + i) % 8]; System.out.println(resultLine + " " + map.get(resultLine)); } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { static List<Map<String, Integer>> records = new ArrayList(); public static String getErrorMsg(String urlErr) { // 文件名称只保留16位 String[] str = urlErr.split("\\\\"); String[] fileAndRow = str[str.length - 1].split(" "); String file = fileAndRow[0]; if (file.length() > 16) file = file.substring(file.length() - 16, file.length()); return file + " " + fileAndRow[1]; } public static void incr(String file) { Map<String, Integer> r = records.stream().filter(p -> p.containsKey(file)).findAny().orElse(null); if (r != null) { r.put(file, r.get(file) + 1); } else { r = new HashMap(); r.put(file, 1); records.add(r); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String str = in.nextLine(); incr(getErrorMsg(str)); } int start = records.size() > 8 ? records.size() - 8 : 0; for (; start < records.size(); ++ start) { Map<String, Integer> t = records.get(start); Set<String> key = t.keySet(); key.forEach(k -> System.out.println(k + " " + t.get(k))); } } }
import java.util.LinkedHashMap; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Map<String, Integer> map = new LinkedHashMap<>(); while (in.hasNextLine()) { String[] arr = in.nextLine().split(" "); String name1 = arr[0].substring(arr[0].lastIndexOf("\\") + 1); String name = name1.length() > 16 ? name1.substring(name1.length() - 16) : name1; String line = arr[1]; String key = name + " " + line; if (map.containsKey(key)) { map.put(key, map.get(key) + 1); } else { map.put(key, 1); } } int count = 0; for (String key : map.keySet()) { if (map.size() - count <= 8) { System.out.println(key + " " + map.get(key)); } count ++; } } }
import java.util.Scanner; import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 List<String> list = new ArrayList<>(); while (in.hasNextLine()) { // 注意 while 处理多个 case String inString = in.nextLine(); String[] split = inString.split("\\\\"); String lastString = split[split.length - 1]; String[] splitLast = lastString.split(" "); int length = splitLast[0].length(); if (length>16){ list.add(splitLast[0].substring(splitLast[0].length()-16)+" "+splitLast[1]); }else { list.add(lastString); } } LinkedHashMap<String, Integer> map = new LinkedHashMap<>(); for (int i = 0; i < list.size(); i++) { String s = list.get(i); if (map.containsKey(s)){ map.put(s,map.get(s) + 1); }else { map.put(s,1); } } int count = 0; for (Map.Entry<String, Integer> entry : map.entrySet()) { if (count >= map.size() -8){ String key = entry.getKey(); String[] split = key.split(" "); Integer value = entry.getValue(); if (split[0].length() > 16){ System.out.println(split[0].substring(split[0].length()-16)+" "+split[1]+ " "+value); }else { System.out.println(key + " "+value); } } count++; } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); LinkedHashMap<String, Integer> linkMap = new LinkedHashMap(); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String [] strArr = in.nextLine().split(" "); String [] s = strArr[0].split("\\\\"); String filename = s[s.length-1]; if(filename.length() > 16){ filename = filename.substring(filename.length() - 16); } String record = filename + " " + strArr[1]; linkMap.put(record, linkMap.getOrDefault(record, 0)+1); } int count = 0; for (Map.Entry<String, Integer> entry : linkMap.entrySet()) { count++; if(linkMap.size() > 8 && count > linkMap.size() - 8) System.out.println(entry.getKey()+" "+entry.getValue()); else if(linkMap.size() <= 8){ System.out.println(entry.getKey()+" "+entry.getValue()); } } } }
public static void maint(String[] args) throws IOException { Map<String, Integer> recMap = new LinkedHashMap<>(); Map<String, Integer> allRecords = new HashMap<>(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = br.readLine()) != null) { String[] errLines = line.split(" "); String filename = errLines[0].substring(errLines[0].lastIndexOf("\\")+ 1); String rowNum = errLines[1]; filename = filename.length() > 16 ? filename.substring(filename.length() - 16) : filename; String key = filename+" " + rowNum; if (!recMap.containsKey(key)) { if (!allRecords.containsKey(key)) { if (recMap.keySet().size() == 8) { String oldKey = recMap.keySet().iterator().next(); allRecords.put(oldKey, recMap.get(oldKey)); recMap.remove(oldKey); } recMap.put(key, 1); allRecords.put(key, 1); } } else { recMap.put(filename+" " + rowNum, recMap.get(filename+" " + rowNum) + 1); } if (recMap.keySet().size()==8) { for (String errKey : recMap.keySet()) { System.out.println(errKey + " " + recMap.get(errKey)); } } } }
import java.util.Scanner;import java.io.File;import java.util.*;// 注意类名必须为 Main, 不要有任何 package xxx 信息public class Main {// 调试时 map里最终输出的结果已经对了,不知道怎么while完以后没下面的map遍历,以后找到了问题再回来改吧,各位大佬可以帮忙看看问题吗public static void main(String[] args) {Scanner in = new Scanner(System.in);Map<String,Map<String,Integer>> map = new HashMap<>();List<String> filePaths = new ArrayList<>();while (in.hasNext()){String line = in.next();int num = in.nextInt();String[] strings = line.split(" ");File file = new File(strings[0]);String filename = file.getName();if (filename.length() >16){filename = filename.substring(filename.length()-16);}String lineNum = String.valueOf(num);Map<String, Integer> map1 = map.get(filename);if (Objects.nonNull(map1) && (!(filePaths.isEmpty()) && !(filePaths.contains(file.getPath())))){if (Objects.nonNull(map1.get(lineNum))) {map1.put(lineNum, map1.get(lineNum).intValue() + 1);}}else {map1 = new HashMap<>();map1.put(lineNum, 1);}filePaths.add(file.getPath());map.put(filename,map1);}for (Map.Entry<String, Map<String, Integer>> stringMapEntry : map.entrySet()) {StringBuffer stringBuffer = new StringBuffer();stringBuffer.append(stringMapEntry.getKey());stringBuffer.append(" ");for (Map.Entry<String, Integer> stringIntegerEntry : stringMapEntry.getValue().entrySet()) {stringBuffer.append(stringIntegerEntry.getKey());stringBuffer.append(" ");stringBuffer.append(stringIntegerEntry.getValue());}System.out.println(stringBuffer.toString());}}}
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case HashMap<String,Integer> record=new HashMap(); List<String> list=new ArrayList(); while(in.hasNextLine()){ String line= in.nextLine(); String str=line.substring(line.lastIndexOf("\\")+1); String [] sp=str.split(" "); if(sp[0].length()>16){ str=sp[0].substring(sp[0].length()-16)+" "+sp[1]; } if(list.contains(str)){ record.put(str,record.get(str)+1); }else{ list.add(str); record.put(str,1); } } int startIndex=list.size()>8?list.size()-8:0; for(int i=startIndex;i<list.size();i++){ String s=list.get(i); System.out.print(s+" "); System.out.println(record.get(s)); } } } }