题解 | #简单错误记录#

简单错误记录

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

#include <bits/stdc++.h>

using namespace std;
//只用输出最后出现的八条错误记录——考虑队列,超过八条时弹出 (双端队列,从后端进,从前端出)
//对相同的错误记录只记录一条,但是错误计数增加——考虑哈希表,键为错误记录,值为错误计数


int main(){
    string str = "";
    unordered_map<string, int> mp;
    deque<string> dq;
    
    while(getline(cin, str)){
        str = str.substr(str.find_last_of('\\') + 1); 
        //cout << str << endl; //cqzlyaszjvlsjmkwoqijggmybr 645
        int spacePos = str.find_last_of(' '); //从0开始的第26个索引处是空格
        //cout << spacePos << endl; //26
        if(spacePos > 16){
            str = str.substr(spacePos - 16);
            //cout << str << endl; //lsjmkwoqijggmybr 645
        }
        
        if(mp.find(str) == mp.end()){
            dq.push_back(str);
        }
        mp[str]++;
        
        //cout << dq.front() << endl; //lsjmkwoqijggmybr 645
        if(dq.size() > 8) dq.pop_front(); //超过八条则 从前面推出
    }
    
    for(auto s : dq){
        cout << s << " " << mp[s] << endl;
    }
    
    return 0;
}
华为题库题解 文章被收录于专栏

牛客华为题库的题解

全部评论

相关推荐

点赞 评论 收藏
分享
05-26 10:24
门头沟学院 Java
qq乃乃好喝到咩噗茶:其实是对的,线上面试容易被人当野怪刷了
找工作时遇到的神仙HR
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 11:20
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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