알고리즘/백준

백준 11235 Polling

등반 2021. 12. 27. 01:16

https://www.acmicpc.net/problem/11235

 

11235번: Polling

Output the name of the candidate with the most votes. If there is a tie, output out all of the names of candidates with the most votes, one per line, in alphabetical order. Do not output any spaces, and do not output blank lines between names.

www.acmicpc.net

문제 해설

가장 많이 등장한 인물의 이름을 출력하는 문제다.

해당 인물이 유일하지 않은 경우, 사전 순으로 정렬해 모두 출력한다.

 

이런 문제는 map[sth]++ 를 이용해 쉽게 해결할 수 있다.

해당 값이 없는 경우 map[sth]까지 입력될 때, map[sth] = 0까지 입력이 되고,

이후 "++"가 계산되어 map[sth] = 1;이 된다.

값이 존재하는 경우엔 "++"만 계산된다.

#include <iostream>
#define fio cin.tie(0)->sync_with_stdio(0)
using namespace std;

#include <map>
#include <vector>
int main(){
    fio;
    int N; cin >> N;
    map<string, int> map_;
    for(int i=0; i<N; i++){
        string S; cin >> S;
        map_[S]++;
    }
    //
    int big = 0;
    map<string, int>::iterator iter;
    for(iter = map_.begin(); iter != map_.end(); iter++){
        big = max((*iter).second, big);
    }
    //
    for(iter = map_.begin(); iter != map_.end(); iter++){
        if((*iter).second == big){
            cout << (*iter).first <<'\n';
        }
    }
    return 0;
}

'알고리즘 > 백준' 카테고리의 다른 글

백준 11507 카드셋트  (0) 2021.12.27
백준 11346 Cornell Party - Retry  (0) 2021.12.27
백준 11116 교통량  (0) 2021.12.27
백준 10689 Hamza  (0) 2021.12.27
백준 10527 Judging Troubles  (0) 2021.12.27