题解 | #简单错误记录#

简单错误记录

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

用vector容器来保存文件和行号拼接成的字符串
用unordered_map来统计相同的错误

#include<iostream>
#include<vector>
#include<unordered_map>
#include<string>
using namespace std;

vector<string> textName;

unordered_map<string, int> um;

void countError(const string& str, const int& n);

int main(){
    string s;
    int num;
    while(cin >> s >> num){
        countError(s, num);
    }
    int i = 0;
    if(textName.size() > 8){
        i = textName.size() - 8;
    }
    for(i; i < textName.size(); ++i){
        cout << textName[i] << " " << um[textName[i]] << endl;
    }
    return 0;
}

void countError(const string& str, const int& n){
    string temp;
    int i = str.size() - 1;
    while(i >= 0 && str[i] != '\\'){
        i--;
    }
    if(str.size() - i - 1 > 16){ //判断文件名是否超过16位
        temp = str.substr(str.size() - 16, 16);
    }else{
        temp = str.substr(i + 1, str.size() - i - 1);
    }
    temp = temp + " " + to_string(n); //将文件名和行号拼接作为key值
    if(um.find(temp) == um.end()){
        textName.push_back(temp);
    }
    um[temp]++;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 15:08
点赞 评论 收藏
分享
05-16 21:14
中南大学 Java
白火同学:说到底就是无实习的秋招、有实习的春招,哪个更难找到工作嘛。 现在离秋招还有两个半月时间,你现在可以一边背八股刷算法,一边投实习简历,看能不能拿到一份7-9月的实习。你这9本和技术栈找实习是够的,那你实习过程中继续优化简历。9月一边实习一边继续投秋招简历。
无实习如何秋招上岸
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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