题解 | 记票统计

记票统计

https://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894

unordered_map是无序的容器,它不会保留插入顺序,所以要定义一个vector来存储输入的顺序 不算难

#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

int main() {
    int n, m;
    cin >> n;
    unordered_map<string, int> candidates;
    vector<string> candidates_order;
    for(int i = 0; i < n; ++i) {
        string name;
        cin >> name;
        candidates.insert({name, 0});
        candidates_order.push_back(name);
    }   

    cin >> m;
    int count_invalid = 0;
    while(m--) {
        string name;
        cin >> name;
        if(candidates.find(name) != candidates.end()) {
            ++candidates[name];
        } else ++count_invalid;
    }
    for(const auto& name: candidates_order) {
        cout << name << " : " << candidates[name] << endl;
    }
    cout << "Invalid : " << count_invalid << endl;
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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