题解 | #简单错误记录#

简单错误记录

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++;
    }
        
}
全部评论

相关推荐

07-02 18:09
门头沟学院 Java
苍穹外卖和谷粒商城这俩是不是烂大街了,还能做吗?
想去重庆的鸽子在吐槽:你不如把这俩做完自己搞明白再优化点再来问 何必贩卖焦虑
点赞 评论 收藏
分享
能干的三文鱼刷了10...:公司可能有弄嵌入式需要会画pcb的需求,而且pcb能快速直观看出一个人某方面的实力。看看是否有面试资格。问你问题也能ai出来,pcb这东西能作假概率不高
点赞 评论 收藏
分享
醉蟀:你不干有的是人干
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 13:05
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务