【华为机试】简单错误记录

/*
题目描述:开发一个简单错误记录功能小模块,能够记录出错的代码所在的文件名称和行号
1、记录最多8条错误记录,循环记录,最后只用输出最后出现的八条错误记录。对相同的错误记录只记录一条,但是错误计数增加。
   最后一个斜杠后面的带后缀名的部分(保留最后16位)和行号完全匹配的记录才做算是”相同“的错误记录。
2、超过16个字符的文件名称,只记录文件的最后有效16个字符;
3、输入的文件可能带路径,记录文件名称不能带路径。
4、循环记录时,只以第一次出现的顺序为准,后面重复的不会更新它的出现时间,仍以第一次为准
输入描述:
每组只包含一个测试用例。一个测试用例包含一行或多行字符串。每行包括带路径文件名称,行号,以空格隔开。
输出描述:
将所有的记录统计并将结果输出,格式:文件名 代码行数 数目,一个空格隔开
*/

 

#include <iostream>
#include <map>
#include <vector>
#include <deque>
using namespace std;
string getFileName(string path)
{
    int i = path.length()-1;
    for(; i >= 0; i--) {
        if(path[i] == '\\') {
            break;
        }
    }
    string name = path.substr(i + 1);
    if(name.size() > 16) name = name.substr(name.size()-16, 16);
    return name;
}
int main()
{
    string path;
    string row;
    map<string,int> myMap;
    vector<string> myVec;
    deque<string> myDeq;
    while(cin >> path){
        cin >> row;
        string fileName = getFileName(path);
        string key = fileName + ' ' + row;
        if(myMap.find(key) != myMap.end()){
            myMap[key]++;
        }else{
            myMap.insert(make_pair(key,1));
        }
        myVec.push_back(key);
    }
    for(vector<string>::iterator itor = myVec.begin();itor != myVec.end();itor++) {
        for(map<string,int>::iterator it = myMap.begin();it != myMap.end();it++) {
            if(it->first == *itor){
                string str = it->first + ' ' + std::to_string(it->second);
                myDeq.push_back(str);
                myMap.erase(*itor);
            }
        }
     }
    while(myDeq.size() > 8){
        myDeq.pop_front();
    }
    for(deque<string>::iterator it = myDeq.begin();it != myDeq.end();it++){
        cout << *it << endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值