[boj] (s4) 1302 베스트셀러

강신현·2022년 4월 2일
0

✅ map

문제

링크

풀이

책마다 개수를 저장해둬야 하므로 map(key : 책이름, value : 개수)을 사용했다.
참고로 가장 많은 개수가 동일한 책의 경우 굳이 사전순으로 정렬해줄 필요없이, map에 저장시 key의 사전순으로 자동 저장되므로 그냥 첫번째 key를 출력해주면 된다.

코드

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

using namespace std;

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

    int n;
    map<string, int> book;

    cin >> n;
    while(n--){
        string str;
        cin >> str;
        book[str] ++;
    }

    int max_cnt=0;
    for(auto i:book){
        max_cnt = max(max_cnt, i.second);
    }
    for (auto i : book){
        if (max_cnt == i.second){
            cout << i.first << "\n";
            break;
        }
    }

    return 0;
}
profile
땅콩의 모험 (server)

0개의 댓글