题目
开发一个简单错误记录功能小模块,能够记录出错的代码所在的文件名称和行号。
处理:
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
输出例子1:
fpgadrive.c 1325 1
题解
//先将所有的字符串存入哈希表,key为字符串,value为<出现顺序,出现次数>,顺序取相同的字符串的最小值,次数一直累加
//排序的话,利用set重写比较器,按次数降序,次数相同则按出现顺序排列
//插入过程利用hash时间复杂度可以认为是O(n)
//排序过程set的是红黑树,可以认为是O(nlgn) ,总的复杂度就是这个了
#include<iostream>
#include<unordered_map>
#include<set>
#include<string.h>
using namespace std;
struct info{//记录出现的顺序,和次数
int rank;
int count;
info(int rank,int count){
this->rank=rank;
this->count=count;
}
};
struct fullinfo{//一条完整的结果,字符串和次数
string file;
int rank;
int count;
fullinfo(string file,int rank,int count){
this->file=file;
this->rank=rank;
this->count=count;
}
};
struct classcomp {//set的比较器
bool operator()(const struct fullinfo& f1,const struct fullinfo& f2){
if(f1.count==f2.count)
return f1.rank<f2.rank;
return f1.count>f2.count;
}
};
typedef struct info INFO;
typedef struct fullinfo FULLINFO;
int main(){
unordered_map<string,INFO> record;
unordered_map<string,INFO>::iterator it;
unordered_map<string,INFO>::const_iterator itfind;
set<FULLINFO,classcomp> ret;
set<FULLINFO,classcomp>::iterator sit;
string linestr;//一行输入
string file;//文件名+行号
int pos;//空格的位置
int i=1;
while(getline(cin,linestr)){
if(linestr.length()==0)
break;
pos=linestr.rfind("\\");
file=linestr.substr(pos+1);//拆分得到最后的filename和count
itfind=record.find(file);//在map中查看是否已经有了该字符串,没有则插入,有则次数加1
if(itfind==record.end()){
INFO tmpi(i,1);
record.insert(pair<string,INFO>(file,tmpi));
}
else{
INFO tmpi(itfind->second.rank,itfind->second.count+1);
record.erase(file);
record.insert(pair<string,INFO>(file,tmpi));
}
i++;
}
for(it=record.begin();it!=record.end();it++){
FULLINFO tmpfull(it->first,it->second.rank,it->second.count);//构建排序的set集合
ret.insert(tmpfull);
}
for(i=0,sit=ret.begin();sit!=ret.end()&&i<8;++sit,++i){//最多输出8条记录,file少于16位
if(file.find(" ")<=16){
cout<<(*sit).file<<" "<<(*sit).count<<endl;
}
else{
cout<<(*sit).file.substr(file.find(" ")-16)<<" "<<(*sit).count<<endl;
}
}
return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool compare(pair<string, int> a, pair<string, int> b){
return a.second > b.second;
}
int main(void){
string input, file;
vector<pair<string, int>> errors;
while (getline(cin, input)){
if (input.size() == 0)
break;
unsigned int f = input.rfind('\\');
file = input.substr(f + 1);
errors.push_back(make_pair(file, 1));
for (int i = 0; i<(errors.size() - 1); i++){
if (errors[i].first == file){
errors[i].second++;
errors.pop_back(); break;
}
}
}
stable_sort(errors.begin(), errors.end(), compare);
int idx = 0;
while (idx<8 && idx<errors.size()){
string check = errors[idx].first;
int t = check.find(' ');
if (t>16)
errors[idx].first.erase(0, t - 16);
cout << errors[idx].first << ' ' << errors[idx].second << endl;
idx++;
}
}