40行c++解决, 代码清晰易懂

简单错误记录

http://www.nowcoder.com/questionTerminal/2baa6aba39214d6ea91a2e03dff3fbeb

40行c++解决,代码清晰易懂

#include<iostream>
#include<vector>
#include<map>

using namespace std;

string decomp(string filepath){
    int end_pos = filepath.size()-1;
    for(; end_pos >= 0; end_pos--){
        if(filepath[end_pos] == '\\') break;
    }
    string file = filepath.substr(end_pos + 1, filepath.size() - end_pos - 1);
    if(file.size() > 16) file = file.substr(file.size()-16, 16);
    return file;
}
int main(){
    string filepath, length;
    map<string, int> m;
    int start_index = 0;
    vector<string> record(8,"");
    while(cin >> filepath >> length){
        string file = decomp(filepath);
        string key = file + " " + length;
        if(m.find(key) == m.end()){
            m[key] = 1;
            record[start_index] = key;
            start_index = (start_index + 1) % 8;
        }else{
            m[key] += 1;
        }
    }
    for(int i = 0; i < 8; i++){
        if(record[start_index] != ""){
            cout << record[start_index] << " " << m[record[start_index]] <<endl;
        }
        start_index = (start_index + 1) % 8;
    }
    return 0;
}
全部评论
这里有个坑,在存储文件名的时候就把超长的字符去掉了,如果两个文件名都超长,最后16个字符一样怎么办?
1 回复 分享
发布于 2021-01-28 23:16
潮了,这里相同的错误记录是从所有的输入里找的,我写的那个是从最近八条里找,我说怎么最后一个错误记录还不输出
点赞 回复 分享
发布于 2021-12-31 00:01
这个循环操作太厉害了
点赞 回复 分享
发布于 2021-12-22 16:44
最妙的就是取余的操作,这个充分体现了循环,感觉在循环队列的时候经常用,但是我没想到
点赞 回复 分享
发布于 2021-10-30 12:49
认为相同
点赞 回复 分享
发布于 2021-04-07 13:54

相关推荐

不愿透露姓名的神秘牛友
07-09 11:30
找工作7个月,投了7000封,3段世界五百强实习,才有一个offer,牛油们肯定比我强吧
码农索隆:不对不对不对,实习经历这么厉害,简历也没少投,问题出在哪呢
点赞 评论 收藏
分享
06-10 23:36
已编辑
首都经济贸易大学 C++
点赞 评论 收藏
分享
07-10 14:08
已编辑
江西农业大学 Java
念旧select:做完把项目放到自己硬盘里给他看,看完拷走
点赞 评论 收藏
分享
评论
23
8
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务