题解 | #简单错误记录#
简单错误记录
http://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb
这种题目就是考题目理解能力和基本功,解题思路很快就有了,但是细节很不好处理,在用迭代器这踩的坑太久了
#include <map>
#include <vector>
#include <string>
#include <set>
using namespace std;
int main()
{
int count = 0;
string str, path;
map<string, int> fileInfo;
vector<string> outPath;
set<string> strSet;
while (getline(cin, str)) {
//如果已经插入,直接下一次不记录
if (strSet.count(str) != 0)
continue;
else
strSet.insert(str);
int count = 0, numIndex = 0;
//空格的下标
int index1 = str.find(' ');
//最后\的小标
int index2 = str.find_last_of('\\');
//如果\之后文件名长度大于16
if (index1 - index2 - 1 > 16)
path = str.substr(index1 - 16, str.length() - index1 + 16);
else
path = str.substr(index2 + 1, str.length() - index2 - 1);
//记录次数
if (fileInfo.count(path) == 0)
fileInfo.insert(map<string, int>::value_type(path, 1));
else
fileInfo[path] += 1;
if (outPath.empty()) {
outPath.push_back(path);
continue;
}
//判断插入输出队里的值是否已经在队列中
for (numIndex = 0; numIndex < outPath.size(); numIndex++) {
if (outPath[numIndex] == path)
break;
}
//如果不在就插入
if (numIndex == outPath.size() && outPath[numIndex - 1] != path) {
outPath.push_back(path);
}
}
//输出最后8个
if (outPath.size() <= 8)
count = 0;
else
count = outPath.size() - 8;
while (count < outPath.size()) {
cout << outPath[count] << " " << fileInfo[outPath[count]] << endl;
count++;
}
}