题解 | #简单错误记录#

简单错误记录

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

用一个map容器存储对应错误信息的重复个数。
用一个队列来模拟8个错误信息存储容器,如果队列中的元素个数达到8个时,如果再存入元素的时候需要弹出队头元素,之后才能够再从队尾插入新的错误信息。
遍历完整个错误信息队列之后,输出此时队列中的错误信息。
代码如下所示:

#include <iostream>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

class Solution{
public:
        queue<pair<string, string>> myQueue;
        map<string, int> myMap;
public:
        void recordErrors(vector<string>& strVec){
            string order;
            string row;
            int count = 0;
            if(strVec.size() == 0){
                return;
            }
            for(string& elem : strVec){
                for(int ix = elem.size() - 1; ix >= 0;){
                    while(elem[ix] != ' '){
                        row += elem[ix];
                        ix--;
                    }
                    ix--;
                    reverse(row.begin(), row.end());
                    while(elem[ix] != '\\' && count < 16){
                        order += elem[ix];
                        ix--;
                        count++;
                    }
                    count = 0;//用来计数文件名字符个数是否到16
                    reverse(order.begin(), order.end());
                    break;
                }

                auto temp = myMap.insert(pair<string, int>(order+row,1));
                if(temp.second){
                    if(myQueue.size() == 8){
                        myQueue.pop();//当记录错误的个数达到8个的时候需要弹出最先存入的数据
                    }
                    myQueue.push(make_pair(order, row));
                }else{
                    myMap[order+row] += 1;//记录出现的次数
                }
                order.clear();
                row.clear();
            }
        }
};

int main(){
    Solution myS;
    string str;
    vector<string> testStr;
    while(getline(cin, str)){
        testStr.push_back(str);
    }
    myS.recordErrors(testStr);
    while(!myS.myQueue.empty()){
            cout << myS.myQueue.front().first << " " << myS.myQueue.front().second << " "
                << myS.myMap[myS.myQueue.front().first+myS.myQueue.front().second] << endl;
            myS.myQueue.pop();
        }
    return 0;
}
全部评论

相关推荐

程序员牛肉:主要是因为小厂的资金本来就很吃紧,所以更喜欢有实习经历的同学。来了就能上手。 而大厂因为钱多,实习生一天三四百的就不算事。所以愿意培养你,在面试的时候也就不在乎你有没有实习(除非是同级别大厂的实习。) 按照你的简历来看,同质化太严重了。项目也很烂大街。 要么换项目,要么考研。 你现在选择工作的话,前景不是很好了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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