题解 | #简单错误记录#
简单错误记录
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;
}
查看12道真题和解析
