首页 > 试题广场 >

简单错误记录

[编程题]简单错误记录
  • 热度指数:336098 时间限制: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不会被记录。  
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <math.h>
typedef struct {
	int num;//行数
	char str[100];
	int cnt;//错误记数次数
}error_log;

int main(){
	int i,len,row,times,Flag,arr_cnt;
	char str[10000];
	char *p,*q;
	error_log *tmp,*ar[10000];

	times = 0;
	Flag = 0;
	arr_cnt = 0;
	tmp = (error_log *)malloc(sizeof(error_log));
	while(scanf("%s %d",tmp->str,&(tmp->num)) !=EOF){	
		p = strrchr(tmp->str,'\\');
		p++;
		len = strlen(p);
		if(len > 16){
			p = p+len-16;
		}
		for(i=0;i<arr_cnt;i++){//比较统计数组,看是否有重复记录,有相同的直接跳出,不需要记录
			if(strcmp(ar[i]->str,p) == 0){
				if(ar[i]->num == tmp->num){
					++ar[i]->cnt;
					break;
				}
			}
		}
		if(arr_cnt == i){
			ar[arr_cnt] = (error_log *)malloc(sizeof(error_log));
			ar[arr_cnt]->num = tmp->num;
			strcpy(ar[arr_cnt]->str, p);//复制字符串
			ar[arr_cnt]->cnt = 1;//times;
			arr_cnt ++; 
		}	
	}
		if(arr_cnt<8)
			i = 0;
		else 
			i = arr_cnt-8;
		for(;i<arr_cnt;i++)
			printf("%s %d %d\n",ar[i]->str,ar[i]->num,ar[i]->cnt);

	return 0;
}


发表于 2016-07-09 13:12:52 回复(5)
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        //LinkedHashMap可以保证按照存入顺序
		Map<String, Integer> map = new LinkedHashMap<String, Integer>();
		int count = 0;
		while (sc.hasNextLine()) {
			String str_1 = sc.nextLine();
			//读取一行  存入一行至map中
			mapDispose(map, str_1);
		}
        // 输出后8位
			for (String st : map.keySet()) {
				count++;
				if (count > (map.size() - 8)) {
					System.out.println(st + " " + map.get(st));
				}
			}
	}
    
	public static void mapDispose(Map<String, Integer> map, String str) {
		// "\\s" 是按照单个空格 、多个空格、tab制表符分割
		String[] strArr = str.split("\\s+");
		// "\\\\"按照反斜杠分割
		String[] nameArr = strArr[0].split("\\\\");
		int num = Integer.parseInt(strArr[1]);
		String name = nameArr[nameArr.length - 1];
		// 取最后有效的16个字符为最终文件名
		if (name.length() > 16) {
			name = name.substring(name.length() - 16);
		}
		// 将文件名和行号组成字符串 作为map的key值存放
		String key = name + " " + num;
		if (map.containsKey(key)) {
			map.put(key, map.get(key) + 1);
		} else {
			map.put(key, 1);
		}
	}
}

发表于 2017-07-09 22:00:40 回复(5)
有2个坑:
1.循环遍历:
只以第一次出现的顺序为准,后面重复的不会更新它的出现时间,仍以第一次为准
2.净文件名:
最后一个斜杠后面的带后缀名的部分,原来是啥就是啥,而不是最后只保留的16位文件名 


import sys

stat = {}
queue = []
for line in sys.stdin:
    vec = line.strip().split()
    file = vec[0].split("\\")[-1]
    line_no = vec[1]
    mark = "%s_%s" % (file, line_no)
    if mark not in queue:
        queue.append(mark)
        stat[mark] = 1
        # 就是栽跟头在这里,不能提前删后8条前面的记录
        (1619)#if len(queue) > 8:
        #    del queue[0]
    else:
        stat[mark] += 1

for mark in queue[-8:]:
    vec = mark.split("_")
    file = vec[0]
    if len(file) > 16: file = file[-16:]
    print("%s %s %s" % (file, vec[1], stat[mark]))


发表于 2020-03-07 03:02:56 回复(1)
import java.util.*;
public class Main{
    public static void main(String[] args){
        // 题目要求按顺序输出最后八条数据,那就要用LinkedHashMap
        // 这样就能按照输入的顺序输出最后八条数据
        Map<String, Integer> map = new LinkedHashMap<>();
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            String str = sc.nextLine();
            str = str.substring(str.lastIndexOf('\\') + 1);
            // 这里的20是将空格和错误码都算进字符串长度了
            if(str.length() > 20){
                str = str.substring(str.length()-20);
            }
            if(map.containsKey(str)){
                map.put(str, map.get(str)+1);
            }
            else{
                map.put(str, 1);
            }
        }
        // 如果输入数据不超过八条就遍历输出
        // 如果超过就跳过之前的数据直到倒数第八条
        if(map.size() <= 8){
            for(Map.Entry<String, Integer> entry : map.entrySet()){
                System.out.print(entry.getKey() + " ");
                System.out.println(entry.getValue());
            }
        }
        else{
            int count = 0;
            for(Map.Entry<String, Integer> entry : map.entrySet()){
                if(count++ < map.size()-8) continue;
                System.out.print(entry.getKey() + " ");
                System.out.println(entry.getValue());
            }
        }
    }
}

发表于 2021-08-05 16:14:30 回复(1)
#include <iostream>
(720)#include <vector>
#include <algorithm>
using namespace std;

struct erro{  //创建一个结构体类型,来记录每条错误的名字name 行数num 错误数cnt
    string name;
    int num;
    int cnt;
};

int find(vector<erro> &v,erro er){  //在v中寻找是否出现过该条记录
    for(int i = v.size()-1;i>=0;i--){
        if(v[i].name == er.name&&v[i].num == er.num){ //如果出现就将原来记录的cnt加1;
            return ++v[i].cnt;
        }
    }
    return 1;  //没有出现则返回1
}

int main(){
    string name;
    int num;
    vector<erro> v;
    while(cin>>name>>num){
        int i = name.length()-1;
        string Name;
        while(name[i]>=0&&name[i] != '\\'&&Name.length()<16){ //选取后16个字符作为名字
              Name.push_back(name[i]);
                i--;
        }
        reverse(Name.begin(),Name.end());  //因为上面是倒序插入,所以这里翻转Name
        erro er;
        er.name = Name;
        er.num = num;
        er.cnt = find(v,er); //判断有没有出现过
        if(er.cnt == 1) v.push_back(er);//如果没有出现过,就将该条记录的加入v中
        
    }
    //输出V
    if(v.size()<=8){
        for(int i = 0;i<v.size();i++){
            cout<<v[i].name<<' '<<v[i].num<<' '<<v[i].cnt<<endl;
        }
    }
     else {
         for(int i = v.size()-8;i<v.size();i++)
             cout<<v[i].name<<' '<<v[i].num<<' '<<v[i].cnt<<endl;
     }
return 0;
}

发表于 2020-02-24 14:12:01 回复(1)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        LinkedHashMap<String,Integer> map = new LinkedHashMap<>();//LinkedHashMap可以保证按照存入顺序
        while(sc.hasNext()){
            String s = sc.nextLine();
            String key = null;
            if(s.contains("\\")){
                key = s.substring(s.lastIndexOf("\\")+1);
            }else{
                key = s;
            }
            if(map.containsKey(key)){//净文件名称和行号完全匹配只记录一条,
                map.put(key,map.get(key)+1);
			}else{
                map.put(key,1);//这里的净文件名,必须记录全名字,之后再处理16位字符的情况
            }
        }
        int count=0;
        for(String string:map.keySet()){
            count++;
            if(count>map.size()-8){//由于实在没有找到map中删除首先加入key的方法,使map循环保留8个size,故采用了别人的输出最后8个的方法。
                String[] ss = string.split(" ");
                if(ss[0].length()>16){// 超过16个字符的文件名称,只记录文件的最后有效16个字符
                    String str = ss[0].substring(ss[0].length()-16)+" "+ss[1];
                    System.out.println(str+" "+map.get(string));
                }else{
                    System.out.println(string+" "+map.get(string));
				}
            }
        }
    }
}

发表于 2017-07-11 10:52:08 回复(0)
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<unordered_map>
using namespace std;

vector<string> SplitString(string str, string spliter)
{
    vector<string> strs;
    str += spliter;
    int step = spliter.size();
    int pos = str.find(spliter);
    while(pos != str.npos)
    {
        strs.push_back(str.substr(0, pos));
        str = str.substr(pos + step, str.size());
        pos = str.find(spliter);
    }
    return strs;
}

int AimPos(vector<string> data, string aim)
{
    int pos(-1);
    for (int i(0); i<data.size(); ++i)
    {
        //只找第一次出现的位置
        if (aim == data[i])
        {
            pos = i;
            break;
        }
    }
    return pos;
}

int main(){
    //读取数据
    vector<string> linestrs;
    string linestr;
    while(getline(cin, linestr))
    {
        int pos = AimPos(linestrs, linestr);
        //发现是之前有过的
        if (pos != -1 && linestrs.size()>8)
        {
            linestrs.erase(linestrs.begin()+pos);
        }
        else
        {
            linestrs.push_back(linestr);
        }
    }
    //分割出目标
    vector<string> outstrs;
    vector<int> counts;
    for (int i(0); i<linestrs.size(); ++i)
    {
        vector<string> temp = SplitString(linestrs[i], "\\");
        string tempstr = temp[temp.size()-1];
        //长度也需要控制一下
        string name = SplitString(tempstr, " ")[0];
        int dlen = name.size()-16;
        if (dlen > 0)
        {
            tempstr = tempstr.substr(dlen, tempstr.size());
        }
        int pos = AimPos(outstrs, tempstr);
        //第一次出现
        if (pos == -1)
        {
            outstrs.push_back(tempstr);
            counts.push_back(1);
        }
        else
        {
            counts[pos] ++;
        }
    }
    //只输出最后8条
    int outsize = outstrs.size();
    int startnum = (outsize-8 > 0 ? outsize-8 : 0);
    for (int i(startnum); i<outstrs.size(); ++i)
    {
        cout << outstrs[i] << " " << counts[i] << endl;
    }
    return 0;    
}

发表于 2022-07-31 14:53:15 回复(0)
import sys
# 初始化处理错误的函数
def error_format(get_str):
    last_str = get_str.split('\\')
    error_str = last_str[-1].replace('\n', '')
    if len(last_str[-1]) > 20:
        return error_str[-20:]
    return error_str
lines = list(sys.stdin.readlines())
# 存储错误信息的哈希表
lst_error = dict()
for error in lines:
    std_error = error_format(error)
    if std_error in lst_error:
        lst_error[std_error] += 1
    else:
        lst_error[std_error] = 1
# 查找最后八个键
n = len(lst_error.keys())
if n > 8:
    lst_error_end8 = list(lst_error.keys())[-8:]
else:
    lst_error_end8 = list(lst_error.keys())                       
for j in lst_error_end8:
    print(j + ' ' + str(lst_error[j]))

发表于 2022-07-25 18:08:00 回复(0)
题目和以往一样写的一样难懂
发表于 2022-07-02 08:19:11 回复(0)
public class Main {
    public static void main(String[] args) throws IOException {
        HashMap<String, Integer> map = new HashMap<>();
        Deque<Result> deque = new ArrayDeque();
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str=null;
        while ((str = bf.readLine())!=null&&!str.equals("")) {
            String[] split = str.split(" ");
            int hang = Integer.parseInt(split[1]);
            int index = split[0].lastIndexOf("\\");
            String name = split[0].substring(index+1);
            String key = name+hang;
            Result result = new Result();
            result.setName(name);
            result.setHang(hang);
            map.put(key,map.getOrDefault(key,0)+1);
            deque.addLast(result);
        }
        int p = 1;//计数器
        while (!deque.isEmpty()){
            Result result = deque.peekLast();
            Integer num = map.get(result.name + result.hang);
            result.setNum(num);
            System.out.println(result);
            p++;
            if (p>8){
                break;
            }
        }
    }

    static class Result{
        String name;
        int hang;
        int num;

        public void setName(String name) {
            this.name = name;
        }

        public void setHang(int hang) {
            this.hang = hang;
        }

        public void setNum(int num) {
            this.num = num;
        }

        @Override
        public String toString() {
            return name +" "+ hang +" "+ num;
        }
    }
}
为什么无法运行啊!求大佬帮我看看
发表于 2022-06-15 12:03:12 回复(0)
import sys

records = {}
for line in sys.stdin:
    file_path, line_num = line.split(" ")
    file = file_path.split("\\")[-1]
    if len(file) > 16:
        file = file[-16:len(file)]
    rd = file + " " + line_num[:-1] # remove the '\n'
    if rd in records.keys():
        records[rd] += 1
    else:
        records.update({rd:1})
keys = list(records.keys())
last_8_keys = keys[-8:len(keys)]
for key in last_8_keys:
    print(key,records[key], sep=" ")
发表于 2022-06-11 18:41:06 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        HashMap<String, Integer> mapCount = new HashMap<>();
        List<String> list = new ArrayList<>();
        String fileName = "";
        while (in.hasNextLine()) {
            String[] s = in.nextLine().split("\\\\");
            String[] str = s[s.length - 1].split(" ");
            if (str[0].length() > 16) {
                fileName = str[0].substring(str[0].length() - 16,
                                            str[0].length()) + " " + str[1];

            } else {
                fileName = str[0] + " " + str[1];
              
            }
            //------------------------------------------------------------------------------------------------------------
            //上面代码用来获取哈希表需要的key,如D:\zwtymj\xccb\ljj\cqzlyaszjvlsjmkwoqijggmybr 645中的qzlyaszjvlsjmkwoqijggmybr 645
            
            //如果错误记录大于等于8且是之前不重复的记录则将最早查到的错误记录给删除即list中的第一条(list可按存放先后顺序排序)
            if (list.size() >= 8 && !mapCount.containsKey(fileName)) {
                list.remove(0);
                list.add(fileName);

            }
            
            //错误记录小于8且list不存在一样的错误记录(错误文件名+行号)才放入
            if (list.size() < 8 && !list.contains(fileName)) {
                list.add(fileName);
            }
            //将每个错问记录的错问计数放入哈希表对应的values
            int count = mapCount.getOrDefault(fileName, 0) + 1;
            mapCount.put(fileName, count);
        }
        //遍历list可获得要求的错误记录的文件名与行号组成字符串,再由这一字符串找到哈希表对应的错误计数,按要求输出即可
        for (String x : list) {
            System.out.println(x + " " + mapCount.get(x));
        }
    }
}
发表于 2022-04-30 23:49:44 回复(0)
sstream真好用,记录一个。
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
#include<unordered_map>
using namespace std;

int main(){
    string str, sub_str, error_str, line_num, key;
    unordered_map<string, int> hash;
    vector<string> err_order;
    while (getline(cin, str)){
        stringstream ss1(str);
        while (getline(ss1, sub_str, '\\'));
        stringstream ss2(sub_str);
        getline(ss2, error_str, ' ');
        getline(ss2, line_num);
        if (error_str.length() > 16){
            error_str.erase(0, error_str.length() - 16);
        }
        key = error_str + ' ' + line_num;
        if (hash.count(key) == 0){
            err_order.push_back(key);
        }
        hash[key]++;
    }
    
    int err_size = err_order.size();
    for (string s : err_order){
        if (err_size-- <= 8){
            cout << s << ' ' << hash[s] << endl;
        }
    }
}

发表于 2022-03-30 16:15:17 回复(0)
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<String> list = new ArrayList();
        while(sc.hasNextLine()){
            String str = sc.nextLine();
            if(str.equals("")){
                break;
            }else{
                list.add(str);
            }
        }
        myfunction(list);
    }

    private static void myfunction(ArrayList<String> list) {
        ArrayList list2 = new ArrayList();
        HashMap<String,Integer> hp = new HashMap();
        for(int i=0;i<list.size();i++){
        int leftindex = list.get(i).lastIndexOf("\\") +1;
        int rightindex = list.get(i).indexOf(" ") - 1;
        String st = rightindex - leftindex + 1 > 16 ?
                list.get(i).substring(rightindex - 15) : list.get(i).substring(leftindex);
            if(hp.containsKey(st)){
                hp.put(st,hp.get(st)+1);
            }else{
                list2.add(st);
                hp.put(st,1);
            }
        }
        int startindex = list2.size() > 8 ? list2.size() - 8 : 0;
        for(int j = startindex ;j<list2.size();j++){
            System.out.println(list2.get(j) + " " + hp.get(list2.get(j)));
        }
    }
}

发表于 2021-10-17 14:16:22 回复(0)
运行时间53ms占用内存12600KB
import java.util.Map;
import java.util.Scanner;
import java.util.LinkedHashMap;

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 input = in.nextLine();
            if (input.length() == 0) {
                break;
            }
            String[] inputArr = input.split("\\s+");
            String[] filePaths = inputArr[0].split("\\\\");
            String fileName = filePaths[filePaths.length - 1];
            String file = fileName.substring(Math.max(fileName.length() - 16, 0));
            String key = file + " " + inputArr[1];
            map.put(key, map.getOrDefault(key, 0) + 1);
        }
        if (map.size() == 0) {
            return;
        }
        int i = 0;
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (map.size() - i > 8) {
                i++;
                continue;
            }
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
    }
}



发表于 2021-08-22 09:08:58 回复(0)
哈希表+队列,哈希表用于计数,队列用于存储最后8条。
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Map<String,Err> map = new HashMap<>();
        Deque<Err> deque = new LinkedList<>();
        String s = null;
        while(sc.hasNextLine()) {
            // 去掉前面的磁盘名
            s = sc.nextLine().substring(3);
            // 拆分文件与行号
            String[] var1 = s.split(" ");
            // 拆分文件层级
            String[] var2 = var1[0].split("\\\\");
            // 获取错误名
            String errName = var2[var2.length - 1];
            // 获取行号
            String row = var1[1];
            
            Err err = map.get(errName + row);
            if(err == null) {
                // 添加新错误
                err = new Err(errName, row);
                map.put(errName + row, err);
                deque.addLast(err);
                if(deque.size() > 8) {
                    deque.pop();
                }
            } else {
                // 存在错误,累加
                err.count++;
            }
        }
        // 开始输出
        while(!deque.isEmpty()) {
            Err pop = deque.pop();
            String name = pop.name;
            if (name.length() > 16) {
                name = name.substring(name.length() - 16);
            }
            System.out.println(name + " " + pop.row + " " + pop.count);
        }
    }
}

class Err {
    public String name;
    public String row;
    public int count;

    public Err(String name, String row) {
        this.name = name;
        this.row = row;
        this.count = 1;
    }
}


发表于 2021-08-01 17:50:35 回复(0)
#include <iostream>
#include <queue>
#include <string>
#include <map>

using namespace std;

int  main()
{
    string eMessage;
    queue<string> q;
    map<string,int> quants;
    int m=8,n=16;
    while(getline(cin, eMessage))
    {
        size_t pos=eMessage.find_last_of("\\");
        string message=eMessage.substr(pos+1,eMessage.size()-pos-1);
        if(quants.find(message)==quants.end())
        {
            quants.insert(make_pair(message, 1));
            q.push(message);
        }
        else
        {
            quants[message]++;
        }
    }
    while(q.size()>m)
    {
        q.pop();
    }
    for(int i=0;i<q.size();i++)
    {
        int num=quants[q.front()];
        string result=q.front();
        size_t pos=result.find(" ");
        if(pos!=string::npos)
        {
            string temp=result.substr(0,pos);
            if(temp.size()>n)
            {
                result=result.substr(temp.size()-n);
            }
            cout<<result<<" "<<num<<endl;
        }
        q.push(q.front());
        q.pop();
    }
 }

编辑于 2021-06-30 01:03:38 回复(0)
#include<iostream>
#include<string>
#include<deque>
#include<unordered_map>
#include<sstream>
#include<algorithm>

using namespace std;

int main()
{
    string str;
    string item;
    deque<string> str_que;//队列,保持先进先出,保存错误类型
    deque<string> temp_que;//暂时队列,保持先进先出,保存每一条数据中属于错误类型的那一项
    unordered_map<string , int> unique_map;//是否存在相同错误类型

    while(getline(cin, str))
    {
        stringstream ss(str);
        
        while(getline(ss , item , '\\'))//按'\'分割数据,获取错误类型
        {
            //cout<<item<<endl;
            temp_que.push_back(item);//先保存每一段字符
        }

        while(temp_que.size() > 1)
            temp_que.pop_front();//只留下最后一段字符也就是错误类型
        str_que.push_back(temp_que.front());//存入错误类型队列中
        temp_que.clear();//清空暂时队列
        
        if( unique_map.count( str_que.back()) ) //如果出现重复错误类型,计数加一
        {
            auto it = unique_map.find(str_que.back());
            int temp = it->second;
            it->second = temp + 1;
            str_que.pop_back();
        }
        else
            unique_map[str_que.back()] = 1;//如果没有出现重复错误类型,插入错误类型

        if(str_que.size() == 9)//如果队列容量大于八,清除最早的错误类型
            str_que.pop_front();
    }

    while(!str_que.empty())
    {
        if(str_que.front().size() <= 20)
        {
            cout<<str_que.front();
        }
        else
        {
            for(int i = str_que.front().size() - 20 ; i < str_que.front().size() ; i++) //超过16个字符的文件名称,只记录文件的最后有效16个字符;
                cout<<str_que.front()[i];
        }
        cout<<' '<<unique_map[str_que.front()]<<endl;
        str_que.pop_front();
    }
}

发表于 2021-06-09 13:41:58 回复(0)
let data = readline();
let filterArr = [];
let obj = {};
while (data) {
    let arr = data.split(' ')
    let str = arr[0].split('\\').pop()
    if(str.length>16){
        str = str.slice(str.length-16)
    }
    let key = str+' '+arr[1]
    if(filterArr.includes(key)){
        obj[key]++
    }else{
        obj[key] = 1
        filterArr.push(key)
    }
    data = readline();
    if(!data){
        filterArr.slice(filterArr.length-8).map(key=>{
            console.log(`${key} ${obj[key]}`)
        })
    }
}
发表于 2021-04-16 14:21:44 回复(0)

终于解决了,看了评论区的回答才明白,题意的循环记录太坑了。

#-*-coding:utf-8-*-
 import sys
table = {}
name = []
for line in sys.stdin:
    path, num = line.split()
    path = path.split("\\")[-1]
    key = path[-16:] + ' ' + num            # 文件名 + 代码行数相同才算"相同"的错误记录。
    if key not in name:
        if key not in table.keys():            # 题意中未阐述清楚的循环记录。若后面出现的会更新第一次出现的时间,此判断注释即可。
            if len(name) == 8:            # name表中只记录8条错误记录
                name.pop(0)
            name.append(key)
            table[key] = 1
        else:            # 已经出现过的记录的值 +1 。因为不输出,不加也不影响check
            table[key] += 1
    else:            # 正常情况计数
        table[key] += 1
# for i, j in zip(range(len(name)), table.keys()):
for i in range(len(name)):
    print(name[i] + ' ' + str(table[name[i]]))
编辑于 2021-04-13 10:55:03 回复(0)