[백준] 16165번 : 걸그룹 마스터 준석이

김개발·2021년 11월 2일
0

백준

목록 보기
65/75

문제 푼 날짜 : 2021-11-01

문제

문제 링크 : https://www.acmicpc.net/problem/16165

접근 및 풀이

C++ STL 중 map을 이용하여 문제를 해결하였다.
key를 걸그룹 팀 명으로 하고, value를 해당 걸그룹의 이름을 저장한 vector로 해주었다.

코드

// 백준 161655번 : 걸그룹 마스터 준석이
#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int N, M;
    map<string, vector<string>> m;
    cin >> N >> M;
    for (int i = 0; i < N; i++) {
        string team;
        int member;
        cin >> team >> member;
        
        for (int j = 0; j < member; j++) {
            string name;
            cin >> name;
            m[team].push_back(name);
        }
    }

    for (int i = 0; i < M; i++) {
        int type;
        string str;
        cin >> str >> type;
        if (type) {
            for (auto it = m.begin(); it != m.end(); it++) {
                if (find(it->second.begin(), it->second.end(), str) != it->second.end()) {
                    cout << it->first << '\n';
                }
            }
        } else {
            for (auto it = m.begin(); it != m.end(); it++) {
                if (it->first.compare(str) == 0) {
                    sort(it->second.begin(), it->second.end());
                    for (string s : it->second) {
                        cout << s << '\n';
                    }
                }
            }
        }
    }
    return 0;
}

결과

피드백

구현문제를 많이 풀어서 조건에 맞게 구현하는 능력을 기르자..

profile
개발을 잘하고 싶은 사람

0개의 댓글