https://www.acmicpc.net/problem/1302
1302번: 베스트셀러
첫째 줄에 오늘 하루 동안 팔린 책의 개수 N이 주어진다. 이 값은 1,000보다 작거나 같은 자연수이다. 둘째부터 N개의 줄에 책의 제목이 입력으로 들어온다. 책의 제목의 길이는 50보다 작거나 같고
www.acmicpc.net
자바로 풀기로 다짐했었는데.. 자바 map 사용이 익숙지 않아서 잠시 c++을 사용하였다.
map<string, int> 정렬 방법
vector<string, int>로 바꾸어서 정렬한다. -> pair 사용!
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
bool compare(pair<string, int>& a, pair<string, int>& b) {
if (a.second == b.second) {
return a.first < b.first;
}
return a.second > b.second;
}
int main() {
int N;
string input;
cin >> N;
map<string, int> m;
for (int i = 0; i < N; i++) {
cin >> input;
m[input]++;
}
vector<pair<string, int>> v(m.begin(), m.end());
sort(v.begin(), v.end(), compare);
cout << v.begin()->first;
return 0;
}
'ALGORITHM > OTHER' 카테고리의 다른 글
[c++] 20291 파일정리 (map사용) (0) | 2021.09.02 |
---|---|
[JAVA] 1719 택배 (다익스트라) (0) | 2021.09.02 |
[Java] 16926 배열 돌리기 1, 16935 배열 돌리기 3 (0) | 2021.08.12 |
[c++] 전화번호 목록 (프로그래머스-Trie, 정렬, 해시) (0) | 2021.07.17 |
[c++] 숫자 문자열과 영단어 (프로그래머스, 구현) (0) | 2021.07.12 |