PAT(A)1039 Course List for Student (25分)(map套vector)

在这里插入图片描述

Sample Input

11 5
4 7
BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
1 4
ANN0 BOB5 JAY9 LOR6
2 7
ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6
3 1
BOB5
5 9
AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6 NON9

Sample Output

ZOE1 2 4 5
ANN0 3 1 2 5
BOB5 5 1 2 3 4 5
JOE4 1 2
JAY9 4 1 2 4 5
FRA8 3 2 4 5
DON2 2 4 5
AMY7 1 5
KAT3 3 2 4 5
LOR6 4 1 2 4 5
NON9 0

思路:
map套vector,然后排序并输出就好。
代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <vector>

using namespace std;

typedef long long ll;
#define error 100001
#define endl '\n'

map<string, vector<int> >mp;

int main()
{
    // ios::sync_with_stdio(false);
    // cin.tie(0); cout.tie(0);
    int n, cla;
    cin >> n >> cla;
    for (int i = 0; i < cla; ++i)
    {
        int c, p;
        string name;

        cin >> c >> p;

        for (int i = 0; i < p ; ++i)
        {
            cin >> name;
            mp[name].push_back(c);
        }
    }
    for (int i = 0; i < n; ++i)
    {
        string name;
        cin >> name;
        sort(mp[name].begin(), mp[name].end());
        cout << name << " " << mp[name].size();
        for (int i = 0; i < mp[name].size(); ++i)
            cout << " " << mp[name][i];
        cout << endl;
    }
    getchar(); getchar();
    return 0;
}

发布了161 篇原创文章 · 获赞 7 · 访问量 7098

猜你喜欢

转载自blog.csdn.net/weixin_43778744/article/details/104047399
今日推荐