简单错误记录
简单错误记录
题目描述
开发一个简单错误记录功能小模块,能够记录出错的代码所在的文件名称和行号。
处理:
1.记录最多8条错误记录,对相同的错误记录(即文件名称和行号完全匹配)只记录一条,错误计数增加;(文件所在的目录不同,文件名和行号相同也要合并)
2.超过16个字符的文件名称,只记录文件的最后有效16个字符;(如果文件名不同,而只是文件名的后16个字符和行号相同,也不要合并)
3.输入的文件可能带路径,记录文件名称不能带路径
处理:
1.记录最多8条错误记录,对相同的错误记录(即文件名称和行号完全匹配)只记录一条,错误计数增加;(文件所在的目录不同,文件名和行号相同也要合并)
2.超过16个字符的文件名称,只记录文件的最后有效16个字符;(如果文件名不同,而只是文件名的后16个字符和行号相同,也不要合并)
3.输入的文件可能带路径,记录文件名称不能带路径
输入描述:
一行或多行字符串。每行包括带路径文件名称,行号,以空格隔开。
文件路径为windows格式
如:E:\V1R2\product\fpgadrive.c 1325
输出描述:
将所有的记录统计并将结果输出,格式:文件名代码行数数目,一个空格隔开,如: fpgadrive.c 1325 1
结果根据数目从多到少排序,数目相同的情况下,按照输入第一次出现顺序排序。
如果超过8条记录,则只输出前8条记录.
如果文件名的长度超过16个字符,则只输出后16个字符
示例1
输入
E:\V1R2\product\fpgadrive.c 1325
输出
fpgadrive.c 1325 1
1 链接:https://www.nowcoder.com/questionTerminal/67df1d7889cf4c529576383c2e647c48 2 来源:牛客网 3 4 #include <iostream> 5 #include <vector> 6 #include <string> 7 #include <algorithm> 8 using namespace std; 9 10 bool compare(pair<string, int> a, pair<string, int> b){ 11 return a.second > b.second; 12 } 13 int main(void){ 14 string input, file; 15 vector<pair<string, int>> errors; 16 while (getline(cin, input)){ 17 if (input.size() == 0) 18 break; 19 unsigned int f = input.rfind('\\'); 20 file = input.substr(f + 1); 21 errors.push_back(make_pair(file, 1)); 22 for (int i = 0; i<(errors.size() - 1); i++){ 23 if (errors[i].first == file){ 24 errors[i].second++; 25 errors.pop_back(); break; 26 } 27 } 28 } 29 stable_sort(errors.begin(), errors.end(), compare); 30 int idx = 0; 31 while (idx<8 && idx<errors.size()){ 32 string check = errors[idx].first; 33 int t = check.find(' '); 34 if (t>16) 35 errors[idx].first.erase(0, t - 16); 36 cout << errors[idx].first << ' ' << errors[idx].second << endl; 37 idx++; 38 } 39 }
STL
1 //先将所有的字符串存入哈希表,key为字符串,value为<出现顺序,出现次数>,顺序取相同的字符串的最小值,次数一直累加
2 //排序的话,利用set重写比较器,按次数降序,次数相同则按出现顺序排列
3 //插入过程利用hash时间复杂度可以认为是O(n)
4 //排序过程set的是红黑树,可以认为是O(nlgn) ,总的复杂度就是这个了
5 #include<iostream>
6 #include<unordered_map>
7 #include<set>
8 #include<string.h>
9 using namespace std;
10 struct info{//记录出现的顺序,和次数
11 int rank;
12 int count;
13 info(int rank,int count){
14 this->rank=rank;
15 this->count=count;
16 }
17 };
18 struct fullinfo{//一条完整的结果,字符串和次数
19 string file;
20 int rank;
21 int count;
22 fullinfo(string file,int rank,int count){
23 this->file=file;
24 this->rank=rank;
25 this->count=count;
26 }
27 };
28 struct classcomp {//set的比较器
29 bool operator()(const struct fullinfo& f1,const struct fullinfo& f2){
30 if(f1.count==f2.count)
31 return f1.rank<f2.rank;
32 return f1.count>f2.count;
33 }
34 };
35 typedef struct info INFO;
36 typedef struct fullinfo FULLINFO;
37 int main(){
38 unordered_map<string,INFO> record;
39 unordered_map<string,INFO>::iterator it;
40 unordered_map<string,INFO>::const_iterator itfind;
41 set<FULLINFO,classcomp> ret;
42 set<FULLINFO,classcomp>::iterator sit;
43 string linestr;//一行输入
44 string file;//文件名+行号
45 int pos;//空格的位置
46 int i=1;
47 while(getline(cin,linestr)){
48 if(linestr.length()==0)
49 break;
50 pos=linestr.rfind("\\");
51 file=linestr.substr(pos+1);//拆分得到最后的filename和count
52 itfind=record.find(file);//在map中查看是否已经有了该字符串,没有则插入,有则次数加1
53 if(itfind==record.end()){
54 INFO tmpi(i,1);
55 record.insert(pair<string,INFO>(file,tmpi));
56 }
57 else{
58 INFO tmpi(itfind->second.rank,itfind->second.count+1);
59 record.erase(file);
60 record.insert(pair<string,INFO>(file,tmpi));
61 }
62 i++;
63 }
64 for(it=record.begin();it!=record.end();it++){
65 FULLINFO tmpfull(it->first,it->second.rank,it->second.count);//构建排序的set集合
66 ret.insert(tmpfull);
67 }
68 for(i=0,sit=ret.begin();sit!=ret.end()&&i<8;++sit,++i){//最多输出8条记录,file少于16位
69 if(file.find(" ")<=16){
70 cout<<(*sit).file<<" "<<(*sit).count<<endl;
71 }
72 else{
73 cout<<(*sit).file.substr(file.find(" ")-16)<<" "<<(*sit).count<<endl;
74 }
75
76 }
77 return 0;
78 }

浙公网安备 33010602011771号