题解 | #记票统计#

记票统计

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

#include <bits/stdc++.h>
#include <ostream>
#include <vector>
using namespace std;

int main() {
    int n;
    cin >> n;
    string x;
    map<string, int> map;
    vector<pair<string, int>> ans;
    for (int i = 0; i < n; ++i) {
        cin >> x;
        // cout << x << " ";
        map[x] = 0;
        ans.push_back({x, 0});
    }
    cin >> n;
    int Invalid = 0;
    for (int i = 0; i < n; ++i) {
        cin >> x;
        // cout << x << " ";
        auto it = map.find(x);
        if (it == map.end()) {
            Invalid++;
        } else {
            map[x]++;
        }
    }
    for (auto &i : map) {
        for(auto &k : ans){
            if(k.first == i.first){
                k.second = i.second;
                break; // 找到相同键后退出内层循环
            }
        }
    }
    for(auto &i : ans){
        cout << i.first << " : " << i.second << endl;
    }
   
    cout << "Invalid : " << Invalid << endl;

    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务