fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
백준 11235 (C++) Polling
최초 업로드: 2025-08-03 09:07:21
최근 수정 시간: 2025-08-03 09:07:49
게시자: rlatjwls3333
카테고리: 백준
조회수: 22
# [Silver V] Polling [문제 링크](https://www.acmicpc.net/problem/11235) ## 문제 설명 <p>Midterm elections are here! Help your local election commission by counting votes and telling them the winner. If more than one candidate ties with the most votes, print out all of their names in alphabetical order.</p> ## 입력 <p>Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. Each test case will begin with an integer n (1 ≤ n ≤ 1,000), indicating the number of votes. The next n lines will hold the votes. The candidates’ names will appear one per line, and consist of between 1 and 20 capital letters only.</p> ## 출력 <p>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.</p> ## 풀이 #### 맵으로 각 후보자의 투표수를 저장하고, 정렬하여 출력하면 된다. ``` c++ #include<bits/stdc++.h> using namespace std; struct element { string s; int cnt; bool operator<(const element e) { if(cnt!=e.cnt) return cnt > e.cnt; if(s.compare(e.s)<0) return true; return false; } }; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; map<string, int> votes; while(n--) { string s; cin >> s; votes[s]++; } vector<element> res; for(auto e : votes) res.push_back({e.first, e.second}); sort(res.begin(), res.end()); for(int i=0;i<res.size();i++) { if(i>0 && res[i].cnt<res[i-1].cnt) break; cout << res[i].s << '\n'; } } ```