首页 > 试题广场 >

简单错误记录

[编程题]简单错误记录
  • 热度指数:339061 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

开发一个简单错误记录功能小模块,能够记录出错的代码所在的文件名称和行号。


处理:


1、 记录最多8条错误记录,循环记录,最后只用输出最后出现的八条错误记录。对相同的错误记录只记录一条,但是错误计数增加。最后一个斜杠后面的带后缀名的部分(保留最后16位)和行号完全匹配的记录才做算是相同的错误记录。
2、 超过16个字符的文件名称,只记录文件的最后有效16个字符;
3、 输入的文件可能带路径,记录文件名称不能带路径。也就是说,哪怕不同路径下的文件,如果它们的名字的后16个字符相同,也被视为相同的错误记录
4、循环记录时,只以第一次出现的顺序为准,后面重复的不会更新它的出现时间,仍以第一次为准

数据范围:错误记录数量满足 ,每条记录长度满足

输入描述:

每组只包含一个测试用例。一个测试用例包含一行或多行字符串。每行包括带路径文件名称,行号,以空格隔开。



输出描述:

将所有的记录统计并将结果输出,格式:文件名 代码行数 数目,一个空格隔开,如:

示例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

说明

由于D:\cfmwafhhgeyawnool 649的文件名长度超过了16个字符,达到了17,所以第一个字符'c'应该被忽略。
记录F:\fop\ywzqaop 631和F:\yay\jc\ywzqaop 631由于文件名和行号相同,因此被视为同一个错误记录,哪怕它们的路径是不同的。
由于循环记录时,只以第一次出现的顺序为准,后面重复的不会更新它的出现时间,仍以第一次为准,所以D:\zwtymj\xccb\ljj\cqzlyaszjvlsjmkwoqijggmybr 645不会被记录。  
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));
        }
    }

发表于 2024-04-17 10:57:33 回复(0)
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)));
        }
    }
}


编辑于 2024-03-23 21:57:17 回复(0)
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 ++;
        }
    }
}

编辑于 2024-03-06 21:09:36 回复(0)
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++;
        }
    }
}

编辑于 2023-12-04 15:43:14 回复(0)
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());
            }
        }
    }
}


发表于 2023-11-20 17:35:54 回复(0)
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));  }
        }
    }
}
发表于 2023-10-30 22:33:01 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<String, Integer> map1 = new HashMap<>();
        List<String> res = new ArrayList<>();
        while (in.hasNextLine()) {
            String s[] = in.nextLine().split(" ");
            String record = s[0].substring(s[0].lastIndexOf("\\") + 1);
            if (record.length() > 16) {
                record = record.substring(record.length() - 16);
            }

            if (map1.containsKey(record + " " + s[1])) {
                map1.put(record + " " + s[1], map1.get(record + " " + s[1]) + 1);
            } else {
                res.add(record + " " + s[1]);
                map1.put(record + " " + s[1], 1);
            }
        }
        int offset = res.size() > 8 ? res.size() - 8 : 0;
        for (int i = offset; i < res.size(); i++) {
            System.out.println(res.get(i) + " " + map1.get(res.get(i)));
        }

    }
}
发表于 2023-08-28 14:02:09 回复(0)
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());
        }
    }
}

发表于 2023-08-27 15:48:53 回复(0)
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));
           }

        }
    }
}

发表于 2023-08-07 21:11:35 回复(0)
import java.util.Scanner;

import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
       LinkedHashMap<String, Integer> log = new LinkedHashMap<>();
        List<String> recordList = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String record = scanner.nextLine().trim();
            String afterProcess;
            int index1 = record.lastIndexOf("\\");
            int index2 = record.lastIndexOf(" ");
            if (index2 - index1 > 16)
                afterProcess = record.substring(index2 - 16);
            else
                afterProcess = record.substring(index1 + 1);
            
            if (!log.containsKey(afterProcess)) {
                log.put(afterProcess, 1);
                recordList.add(afterProcess);
            } else {
                log.put(afterProcess, log.get(afterProcess) + 1);
            }
            
        }
        if (recordList.size() <= 8){
            for (int i = 0 ; i < recordList.size(); i++){
                System.out.println(recordList.get(i) + " " +  log.get(recordList.get(i)));
            }
        } else {
            for (int i = recordList.size() - 8; i < recordList.size(); i++) {
                System.out.println(recordList.get(i) + " " +  log.get(recordList.get(i)));
            }
        }
    }
}


发表于 2023-07-21 14:53:42 回复(0)
import java.util.Scanner;
import java.util.LinkedHashMap;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String inputStr = in.nextLine();
            String str = getString(inputStr);
            // System.out.println(getString(inputStr));

            map.put(str, map.getOrDefault(str, 0) + 1);
        }

        int i = 0;
        int startIndex = map.size() - 8;
        for (String str : map.keySet()) {
            i++;
            if (i > startIndex) {
                System.out.println(str + " " + map.get(str));
            }
        }
    }

    public static String getString(String inputString) {
        String result = new String();
        String[] tempStrings = inputString.split("\\\\");
        String tempString = tempStrings[tempStrings.length - 1];
        String[] tempStrings2 = tempString.split(" ");
        String tempString2 = tempStrings2[0];

        if (tempString2.length() <= 16) {
            result = tempString;
        } else {
            result = tempString2.substring(tempString2.length() - 16,
                                           tempString2.length()) + " " + tempStrings2[1];
        }

        return result;
    }
}

发表于 2023-07-02 23:30:59 回复(0)
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Path> listOfND = new ArrayList<>();
        while (sc.hasNextLine()) {
            String path0 = sc.next();
            String line = sc.next();
            String path = cut(path0);
            Path p = new Path(path, line);
            listOfND.add(p);
        }
        for (int i = 0; i < listOfND.size() - 1; i++) {
            for (int j = i + 1; j < listOfND.size(); j++) {
                if (listOfND.get(i).equals(listOfND.get(j))) {
                    listOfND.get(i).addNum();
                    listOfND.remove(j);
                }
            }
        }
        for (int i = Math.max(0, listOfND.size() - 8); i < listOfND.size(); i++) {
            System.out.println(listOfND.get(i).toString());
        }
    }

    public static String cut(String str) {
        String newString = "";
        for (int i = str.length() - 1; i >= str.length() - 16; i--) {
            if (str.charAt(i) != '\\') {
                Character c  = str.charAt(i);
                newString = c.toString() + newString;
            } else {
                break;
            }
        }
        return newString;
    }
    public static class Path {
        String path;
        String line;
        Integer num;
        public Path(String s1, String s2 ) {
            this.path = s1;
            this.line = s2;
            this.num = 1;
        }
        public boolean equals(Path p) {
            if (this.path.equals(p.path) && this.line.equals(p.line)) {
                return true;
            }
            return false;
        }
        public void addNum() {
            this.num++;
        }
        public String toString() {
            String result = "";
            result = result + this.path + " " + line + " " + num.toString();
            return result;
        }
    }
}
我的代码在本地IDEA跑是没有问题的,没有报错且输出是正确的。但是放到牛客的环境里面来跑就报错退出了。实在是太奇怪了,我看了一上午都没看出来哪里会报错。求大佬指点。
发表于 2023-06-05 22:37:59 回复(0)
华为OD深圳/西安岗位,部门急招,薪资20-80w. 部门有专门机考辅导人员,每周开视频讲座。欢迎叨扰:***********
编辑于 2023-05-10 15:58:43 回复(0)
import java.util.ArrayList;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static class Wrong {
        public String name;
        public int line;
        public int count = 0;

        public Wrong (String name, int line, int count) {
            this.name = name;
            this.line = line;
            this.count = count;
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<Wrong> fileList = new ArrayList<>();
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.next();
            int line = in.nextInt();
            int index = a.lastIndexOf('\\');
            String fileName = a.substring(index + 1);
            int length = fileName.length();
            if (length > 16) {
                fileName = fileName.substring(length - 16);
            }
            boolean isAdded = false;
            for (int i = 0; i < fileList.size(); i++) {
                if (fileName.equals(fileList.get(i).name) && fileList.get(i).line == line) {
                    fileList.get(i).count++;
                    isAdded = true;
                    break;
                }
            }
            if (!isAdded) {
                fileList.add(new Wrong(fileName, line, 1));
            }
        }
        int size = fileList.size();
        // 以防没有8条错误记录的情况
        int i = size >= 8 ? size - 8 : 0;
        for (; i < size; i++) {
            Wrong file = fileList.get(i);
            System.out.println(file.name + " " + file.line + " " + file.count);
        }
    }
}
这题倒是没有什么坑,就是在输入的结束方式上纠结了好一会
发表于 2023-04-27 16:49:47 回复(0)
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        Map<String, Integer> map = new HashMap<String, Integer>();
        List<String> list = new ArrayList<String>();//记录非重复的键值,按顺序排序
        Map<String, Integer> gabmap = new HashMap<String, Integer>(); //垃圾箱数据
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            String[] strs = str.split(" ");
            String filePath = strs[0];
            String lineNum = strs[1];
            String[] paths = filePath.split("\\\\");
            String lastpath = paths[paths.length - 1];
            if (lastpath.length() > 16) {
                lastpath = lastpath.substring(lastpath.length() - 16, lastpath.length());
            }
            String key = lastpath.concat("###").concat(lineNum);
            if (!gabmap.containsKey(key)) {//垃圾箱中没有的数据再加入
                if (map.containsKey(key)) {
                    map.put(key, map.get(key) + 1);
                } else {
                    map.put(key, 1);
                    list.add(key);//向list中顺序添加非重复数据
                }
            }

            if (map.size() > 8) {
                String head = list.get(0);
                gabmap.put(head,1);//将数据抛入垃圾箱
                map.remove(head);
                list.remove(0);
            }
        }
        for (int i = 0; i < list.size(); i++) {
            String path = list.get(i);
            int line = map.get(path);
            String[] paths = path.split("###");
            System.out.println(paths[0] + " " + paths[1] + " " + line);
        }

    }
发表于 2023-04-18 20:45:18 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<String, Integer> map = new LinkedHashMap<String, Integer>();
        Set<String> set = new LinkedHashSet<String>();

        while (in.hasNextLine()) {
            String str = in.nextLine();
            set.add(str);
        }
        set.forEach(str -> {
            int i = str.lastIndexOf("\\");
            String key = str.substring(i + 1);
            if (key.lastIndexOf(" ") - 16 > 0) {
                key = key.substring(key.lastIndexOf(" ") - 16);
            }
            map.compute(key, (k, v)->v == null ? 1 : v + 1);

        });
        int i = 0;
        int start = 0;
        if (map.size() > 8) start = map.size() - 8;
        for (Map.Entry<String, Integer> e : map.entrySet()) {
            if (i < start) {
                i++;
                continue;
            }
            String key = e.getKey();
            System.out.println(key + " " + e.getValue());
        }

    }
}


发表于 2023-03-23 16:07:42 回复(0)
采用LinkedHashMap类存储,最后遍历可以再优化
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        LinkedHashMap<String,Integer> result = new LinkedHashMap<>();
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            String youxiao = test(a);
            if(result.containsKey(youxiao)){
                Integer b = result.get(youxiao);
                b++;
                result.put(youxiao,b);
                continue;
            }

            result.put(youxiao,1);

        }
        int ora = 0;
        if(result.size() > 8){
            ora = result.size() -8;
        }
        int i = 0;
        for(String c : result.keySet()){
            if(i < ora){
                i++;
                continue;
            }
            System.out.println(c+ " " + result.get(c));
            i++;
        }
    }


    //字符串解析
    public static String test(String beforeStr){
        int weizhi = beforeStr.lastIndexOf("\\");
        String afterStr = beforeStr.substring(weizhi+1);
        String[] afterStr1 = afterStr.split(" ");
        if(afterStr1[0].length()>16){
            afterStr1[0] = afterStr1[0].substring(afterStr1[0].length()-16);
        }
        return afterStr1[0]+" "+afterStr1[1];
    }
}


发表于 2022-12-31 18:06:46 回复(0)
链接:https://www.nowcoder.com/questionTerminal/2baa6aba39214d6ea91a2e03dff3fbeb
来源:牛客网

//这题需要注意的就是不能在IDEA上进行自测,全部输入完毕之后,才能输出最后8条记录,即输出要放在while(sc.hasNext())循环外。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<String> list = new ArrayList<>();
        HashMap<String, Integer> map = new HashMap<>();
        while (sc.hasNext()) {
            //文件名
            String curStr = sc.next();
            //行号
            String num = sc.next();
            //进行文件名预处理
            String[] strArr = curStr.split("\\\\");
            //取最后一个\之后的内容
            curStr = strArr[strArr.length - 1];
            //判断文件名长度是否大于16
            if (curStr.length() > 16) {
                curStr = curStr.substring(curStr.length() - 16);
            }
            //加上行号
            curStr += " " + num;
            //判断是否在map中第一次出现
            if (!map.containsKey(curStr)) {
                list.add(curStr);
                map.put(curStr, 1);
            } else {
                map.put(curStr, map.get(curStr) + 1);
            }
        }
        //打印最后8条记录
        int start = 0;
        if (list.size() > 8) {//假设有9条记录,那么应该从1开始打印,到下标为8结束,共打印这8条记录
            start = list.size() - 8;
        }
        for (; start < list.size(); start++) {
            System.out.println(list.get(start) + " " + map.get(list.get(start)));
        }
    }
}

发表于 2022-12-30 14:53:31 回复(0)
写代码15分钟,看题花了半小时,关键是您吧题目描述清楚这么难吗?题目难道是AI出的?规则写的让人看不明白。不吐槽了,我的思路:
1、第一步:把记录按照要求先处理掉:
a、只保留最后一个斜线后面的字符;b、只保留16个字符,之前的部分截断;
2、用LinkedHashMap记录相同记录出现的次数
3、只输出最后8个记录,遍历时加个计数器,前面size-8个不打印。
直接上代码,提交通过的:
   
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
list.add(in.nextLine());
}
//处理文件名
int len = 16;
try{
for(int i=0; i< list.size(); i++){
String[] arrStr = list.get(i).split(" ");
// String[] files = arrStr[0].split("\\");
// String name = files[files.length -1];
int index = arrStr[0].lastIndexOf("\\");
String name = arrStr[0].substring(index+1);
// System.out.println("name = " + name);
if(name.length() > len){
name = name.substring(name.length()-len);
}
list.set(i, name + " " + arrStr[1]);
}
}catch(Exception e){
e.printStackTrace();
}
//循环加入LinkedHashMap
for(int i=0; i< list.size(); i++){
String key = list.get(i);
if(!map.containsKey(key)){
map.put(key, 1);
}else{
int num = map.get(key);
map.put(key, num + 1);
}
}
//遍历
int size = map.size();
int startIdx = 0;
int counter = 0;
if(size > 8){
startIdx = size - 8;
}
Iterator<String> iterator = map.keySet().iterator();
while(iterator.hasNext()){
String key = iterator.next();
int value = map.get(key);
if(counter >= startIdx){
System.out.println(key + " " + value);
}
counter++;
}
}
}







发表于 2022-12-21 18:00:33 回复(0)
本题关键是使用
LinkedHashMap
默认的HashMap每次查找替换会搞乱顺序
发表于 2022-11-05 14:30:15 回复(0)

问题信息

难度:
108条回答 66141浏览

热门推荐

通过挑战的用户

查看代码