题解 | #简单错误记录#
简单错误记录
https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb
#include <iostream>
#include <unordered_set>
#include <deque>
#include <math.h>
using namespace std;
struct Ans{
string name;
int line;
int count;
};
bool Check(Ans Ans1_Temp, deque<Ans>&ans){//检查Ans1_Temp是否已经在ans中出现过。
deque<Ans>::iterator m1_Iter;
m1_Iter = ans.begin();
while(m1_Iter != ans.end()){
if(m1_Iter->name == Ans1_Temp.name && m1_Iter->line == Ans1_Temp.line){//重复了
m1_Iter->count++;
return false;
}
m1_Iter++;
}
return true;
}
int main() {
unordered_set<string> trash;
deque<Ans> ans;
Ans Ans1_Temp;
string m1_Str;
string str1_Temp;
int num = 0;
while(getline(cin, m1_Str)){
Ans1_Temp.count = 0;
Ans1_Temp.line = 0;
int i = m1_Str.length() - 1, j;
while(m1_Str[i] != '\\') i--;
j = i;
while(m1_Str[j] != ' ') j++;
if(j - i -1 <= 16){
str1_Temp = m1_Str.substr(i + 1, j - i -1);
}
else{
str1_Temp = m1_Str.substr( j - 16, 16);
}
Ans1_Temp.name = str1_Temp;
i = m1_Str.length() - 1;
while(m1_Str[i] != ' ') i--;
str1_Temp = m1_Str.substr(i + 1, m1_Str.length()-1);
j = 0;
while(j <= str1_Temp.length() - 1){
Ans1_Temp.line += ((int)str1_Temp[j] - 48) * pow(10, str1_Temp.length() - j - 1);
j++;
}
Ans1_Temp.count++;
// cout << Ans1_Temp.name << " " << Ans1_Temp.line << " " << Ans1_Temp.count << endl;
str1_Temp = Ans1_Temp.name + str1_Temp;
if(trash.count(str1_Temp) == 0){//在垃圾回收站中的不要了。
if(Check(Ans1_Temp, ans)){//Check是否已经出现过了。
if(num >= 8){//没出现过,此时若已经记录了8条信息,头pop,尾push
str1_Temp = ans.front().name + to_string(ans.front().line);
trash.insert(str1_Temp);
ans.pop_front();
ans.push_back(Ans1_Temp);
}else{//没出现过,此时记录未满8条,直接尾push
ans.push_back(Ans1_Temp);
num++;
}
}
}
// cout << num << endl;
}
deque<Ans>::iterator ans_iter;
ans_iter = ans.begin();
while(ans_iter != ans.end()){
cout << ans_iter->name << " " << ans_iter->line << " " << ans_iter->count << endl;
ans_iter++;
}
return 0;
}
这道题目理解上不难,就是流程上麻烦点,对字符处理要细致
#23届找工作求助阵地##我的实习求职记录#

