#include <iostream> #include <queue> #include <map> using namespace std; struct Info { string name; string linenum; int times; }; // 题目要求保存所有的记录,但只打印最后8条,而不是将之前的记录删除! int main() { string line; map<string, Info*> m; queue<Info*> q; while(getline(cin, line)) { string filepath, linenum; string index; Info* item; int split; item = new Info; split = line.find(' '); filepath = line.substr(0, split); linenum = line.substr(split+1, line.size() - split - 1); split = filepath.rfind('\\'); if (split >= 0) { int si***(16, (int)(filepath.size() - split - 1)); item->name = filepath.substr(filepath.size() - size, size); } else { if (filepath.size() > 16) { item->name = filepath.substr(filepath.size() - 16, 16); } else { item->name = filepath; } } item->linenum = linenum; item->times = 1; index = item->name + item->linenum; // 新item if (m.count(index) == 0) { m[index] = item; // cout << "save " << item << " to " << index << endl; // 超过存储长度 if (q.size() >= 8) { Info* old_item = q.front(); // m.erase(old_item->name + old_item->linenum); m[old_item->name + old_item->linenum] = nullptr; delete old_item; q.pop(); } q.push(item); // 已存在的item } else if (m[index] != nullptr){ m[index]->times++; } } while(!q.empty()) { Info* item = q.front(); printf("%s %s %d\n", item->name.c_str(), item->linenum.c_str(), item->times); delete item; q.pop(); } return 0; }