题解 | #简单错误记录#

简单错误记录

https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb

利用哈希表和链表分别保存总记录信息和最近8条记录信息
#include <bits/stdc++.h>

using namespace std;

using type=pair<string,string>;

struct hash_func {
    size_t operator()(const type& obj)const {
        hash<string> str_hash;
        return str_hash(obj.first+obj.second);
    }
};

int main() {
    string str;
    unordered_map<type,int,hash_func> dict;
    list<type> ret_list;
    while(cin >> str) {
        string count;
        cin>>count;
        string key;
        int n=str.size();
        for(int i=n-1;i>=0;--i) {
            if(str[i]=='\\' || i<n-16) break;
            key.push_back(str[i]);
        }
        reverse(key.begin(),key.end());
        if(dict.count({key,count}) == 0) {
            ret_list.emplace_back(key,count);
        }
        ++dict[{key,count}];
        if(ret_list.size() > 8) {
            ret_list.pop_front();
        }
    }
    for(auto& p:ret_list) {
        cout<<p.first<<" "<<p.second<<" "<<dict[p]<<"\n";
    }
    return 0;
}


#C/C++#
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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