题解 | #简单错误记录#
简单错误记录
https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <unordered_map>
using namespace std;
int findByKey(vector< pair<string, int> > v, string key) {
if (v.empty()) {
return -1;
}
for (int i = 0; i < v.size(); i++) {
if (v[i].first == key) {
return i;
}
}
return -1;
}
int main() {
string input;
vector< pair<string, int> > test;
// unordered_map<string, int> test;
while (getline(cin, input)) {
string s1, s2;
istringstream s(input);
while (getline(s, s1, '\\'));
vector<string> v_tmp;
istringstream ss(s1);
while (getline(ss, s2, ' ')) {
v_tmp.push_back(s2);
}
if (v_tmp[0].size() > 16) {
v_tmp[0] = v_tmp[0].substr(v_tmp[0].size() - 16);
}
string temp = v_tmp[0] + ' ' + v_tmp[1];
if (test.empty()) {
test.push_back(make_pair(temp, 1));
} else {
int i = findByKey(test, temp);
if (i == -1) {
test.push_back(make_pair(temp, 1));
} else {
test[i].second++;
}
}
}
if (test.size() <= 8) {
for (int i = 0; i < test.size(); i++) {
cout << test[i].first << ' ' << test[i].second << endl;
}
}
if (test.size() > 8) {
for (int i = test.size() - 8; i < test.size(); i++) {
cout << test[i].first << ' ' << test[i].second << endl;
}
}
}
// 64 位输出请用 printf("%lld")


字节跳动工作强度 1201人发布