선택은 나의 것

[BOJ 백준] 10546번 배부른 마라토너 본문

☽ Algorithm/BOJ

[BOJ 백준] 10546번 배부른 마라토너

Algoribi 2020. 5. 31. 20:21

문제

BOJ 10546 : https://www.acmicpc.net/problem/10546

 

10546번: 배부른 마라토너

문제 마라토너라면 국적과 나이를 불문하고 누구나 참가하고 싶어하는 백준 마라톤 대회가 열린다. 42.195km를 달리는 이 마라톤은 모두가 참가하고 싶어했던 만큼 매년 모두가 완주해왔다. 단,

www.acmicpc.net

접근

이 문제에서 배부른 마라토너는 단 한 명이다. 따라서 참가자를 전부 받은 다음 완주한 사람을 빼주면 배부른 마라토너만이 남을 것이다. 이때 동명이인은 map의 second인자를 통해 카운트해줬다.

 

코드

// algorithm study
// BOJ_10546 배부른 마라토너

#include <iostream>
#include <map>

using namespace std;

int main() {
    int n;
    cin >> n;
    map<string, int> m;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        if (m.find(s) != m.end())
            m[s]++;
        else
            m.insert(make_pair(s, 1));
    }
    for (int i = 0; i < n - 1; i++) {
        string s;
        cin >> s;
        if (m.find(s)->second == 1)
            m.erase(s);
        else
            m[s]--;
    }
    cout << m.begin()->first;
    return 0;
}

 

깃 허브 주소 : https://github.com/algoribi/algorithm-study

Comments