本题将会给出
条报错信息,确切数字未知,您需要一直读入直到文件结尾;您也可以参考 牛客网在线判题系统使用帮助 获得更多的使用帮助。每条报错信息描述如下:
在一行上先输入一个长度为
的字符串
代表文件路径;随后,在同一行输入一个整数
代表行号。
文件路径的格式如题干所述,保证文件名不为空。
至多八行,每行先输出一个长度为
的字符串
,代表文件名;随后,在同一行输出错误行号、报错次数。
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 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]))
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; } } } 为什么无法运行啊!求大佬帮我看看
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=" ")
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()); } } }
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; } }
#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(); } }
终于解决了,看了评论区的回答才明白,题意的循环记录太坑了。
#-*-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]]))
//常规思路,善用数据结构,主要是队列和哈希 #include <iostream> #include <string> #include <vector> #include <unordered_map> #include <queue> using namespace std; /* 运行时间:6ms 超过5.33%用C++提交的代码 占用内存:492KB 超过47.31%用C++提交的代码 */ int main(){ string filepath; int line; queue<string> errlog; unordered_map<string, int> hash; while(cin >> filepath >> line) { int pos = filepath.find_last_of('\\'); string filename = filepath.substr(pos+1); if(filename.length() > 16) { string tmp = filename.substr(filename.length()-16); filename = tmp; } string logstr = filename + " " + to_string(line); if(hash.count(logstr)) { hash[logstr] ++; } else { //一开始理解错了,以为是动态维护8个错误记录 //实际按题意,先统计错误记录,最后输出最新的8个即可 // if(errlog.size() >= 8) { // string log = errlog.front(); // hash.erase(log); // errlog.pop(); // } errlog.push(logstr); hash[logstr] ++; } } //记录多于8个,弹出旧的记录直到剩下8个 while(!errlog.empty() && errlog.size() > 8) { //cout << errlog.front() << " " << hash[errlog.front()] << endl; string tmp = errlog.front(); hash.erase(tmp); errlog.pop(); } while(!errlog.empty()) { cout << errlog.front() << " " << hash[errlog.front()] << endl; errlog.pop(); } return 0; }
from collections import defaultdict counter = defaultdict(lambda: 0) while True: try: content = input().strip().split(' ') filename = content[0].split("\\")[-1][-16:] # 文件名截取最后16个字符 counter[f"{filename} {content[1]}"] += 1 except: break for k in list(counter.keys())[-8:]: print(f"{k} {counter[k]}")
#include <iostream> #include <string> #include <vector> using namespace std; class Error{ public: string m_name; //错误文件名 int m_line = 0; //错误所在行 int num = 1; // 相同错误计数 }; int main() { string str; int line = 0; //错误所在行 vector<Error> vec; // 记录所有错误 Error temp; int count = 0; // 总错误文件数量计数,用于输出最后8个错误 int flag = 0; //0:与之前文件未重复 , 1:与之前文件重复 while(cin >> str && cin >> line ){ int index = str.rfind("\\"); //查找 '\'字符位置 temp.m_name = str.substr(index+1); //截取文件名 temp.m_line = line; // 记录错误所在行 // 遍历vector判断是否有重复,如果重复了,flag标记为1 for(int i = 0; i< count ;i++){ if(temp.m_name == vec[i].m_name && temp.m_line == vec[i].m_line){ vec[i].num ++; flag = 1; break; } } //如果没有重复,即flag为0,则记录下来,count++ if(flag == 0){ vec.push_back(temp); count++; } flag = 0; } //输出分两种,多于8组数据和少于8组数据。如果多于8组,输出最后8组。少于8组全部输出 if(vec.size()>8){ for(int i = count - 8 ;i< vec.size();i++){ int lenth = vec[i].m_name.size(); string name_temp; // 如果文件名多于16个字符,截取最后16个。否则记录全部 if(lenth > 16){ name_temp = vec[i].m_name.substr(lenth - 16); } else name_temp = vec[i].m_name; //输出 cout << name_temp<< " "<< vec[i].m_line << " "<< vec[i].num<<endl; } } else{ for(int i = 0 ;i< vec.size();i++){ int lenth = vec[i].m_name.size(); string name_temp; if(lenth > 16){ name_temp = vec[i].m_name.substr(lenth - 16); } else name_temp = vec[i].m_name; cout << name_temp<< " "<< vec[i].m_line << " "<< vec[i].num<<endl; } } return 0; }
#include <iostream> #include <string> #include <vector> #include <sstream> #include <algorithm> using namespace std; //获取净文件名 string getFileName(string path){ size_t pos = path.rfind('\\'); return path.substr(pos + 1); } //保留净文件名的后16位 string modifyName(string name){ if(name.size() > 16){ name = name.substr(name.size() - 16); } return name; } struct ErrRecord { string _file; int _lineNo; int _count; ErrRecord(string file, int lineNo):_file(file), _lineNo(lineNo){ _count = 1; } // 后续需要进行查找, 所以先定义好两个错误记录什么情况算相等. bool operator==(const ErrRecord& ER){ return (_file == ER._file) && (_lineNo == ER._lineNo); } }; int main(){ string file; int lineNo; vector<ErrRecord> vER; while(cin >> file >> lineNo){ ErrRecord ER(getFileName(file), lineNo); auto res = find(vER.begin(), vER.end(), ER); if(res == vER.end()){ vER.push_back(ER); } else{ ++res->_count; } } int i = 0; if(vER.size() > 8){ i = vER.size() - 8; } for(; i < vER.size(); ++i){ cout << modifyName(vER[i]._file) << " " << vER[i]._lineNo << " " << vER[i]._count << endl; } return 0; }
import java.util.*; class Item { String fileName; String rowNum; int order; public Item(String fileName, String rowNum, int order){ this.fileName = fileName; this.rowNum = rowNum; this.order = order; } @Override public boolean equals(Object item){ if(!(item instanceof Item)) return false; return ((Item)item).fileName.equals(this.fileName) && ((Item)item).rowNum.equals(this.rowNum); } @Override public int hashCode() { return Objects.hash(fileName, rowNum); } } public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); Map<Item, Integer> map = new HashMap<>(); int order = 0; while(sc.hasNext()){ String[] ss = sc.nextLine().trim().split(" "); String[] temp = ss[0].split("\\\\"); ss[0] = temp[temp.length - 1]; if(ss[0].length() > 16) ss[0] = ss[0].substring(ss[0].length() - 16); Integer i; if((i = map.get(new Item(ss[0], ss[1], order))) != null) map.put(new Item(ss[0], ss[1], order), i + 1); else map.put(new Item(ss[0], ss[1], order), 1); order++; } Set<Item> keys = map.keySet(); Object[] keyList = keys.toArray(); Arrays.sort(keyList, (o1, o2) -> ((Item)o1).order - ((Item)o2).order); int count = keyList.length >= 8 ? keyList.length - 8 : 0; for(; count < keyList.length; count++){ Item i = (Item)keyList[count]; System.out.println(i.fileName + " " + i.rowNum + " " + map.get(i)); } } }
#include <iostream> #include <string> #include <vector> #include <algorithm> //由于数据比较多,可以定义一个结构体来辅助处理,且需要虫重载 == ,因为后面需要做判断 //将切割后的文件名,和行号,以及数目 按照条件(通过find函数来判断是否重复)进入vector中 //最后输出最后8个记录的信息 using namespace std; string getfilename(string str) { int index = str.find_last_of("\\"); string tmp = str.substr(index+1); if(tmp.size() > 16) tmp = tmp.substr(tmp.size()-16); return tmp; } struct errorstr { string filename; int fileno; int count; errorstr(string filename,int fileno) { this->filename = filename; this->fileno = fileno; this->count = 1; } bool operator==(const errorstr & a) { return a.filename == filename && a.fileno == fileno; } }; int main() { string str; int n; vector<errorstr> vec; while(cin>>str>>n) { errorstr er(getfilename(str),n); auto res = find(vec.begin(),vec.end(),er); if(res == vec.end()) { vec.push_back(er); } else res->count++; } if(vec.size() > 8) { for(int i = vec.size()-8;i<vec.size();i++) { cout << vec[i].filename << " " << vec[i].fileno << " " << vec[i].count << endl; } } else { for(int i = 0;i<vec.size();i++) { cout << vec[i].filename << " " << vec[i].fileno << " " << vec[i].count << endl; } } 提交观点 return 0; }