Coding_Test_C++

BaekJoon 1302번: 베스트셀러

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

 

1302번: 베스트셀러

첫째 줄에 오늘 하루 동안 팔린 책의 개수 N이 주어진다. 이 값은 1,000보다 작거나 같은 자연수이다. 둘째부터 N개의 줄에 책의 제목이 입력으로 들어온다. 책의 제목의 길이는 50보다 작거나 같고

www.acmicpc.net

풀이법

1) C++의 장점인 STL을 활용하였다.

2) map을 활용한다면 쉽게 풀 수 있다. auto로 반복하여, 최대 값을 찾고 해당하는 값을 출력하면 되는 문제이다.

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <cstring>

#include<map>
using namespace std;
int n;
map<string, int> m;
int cnt;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		string s;
		cin >> s;
		m[s]++;
	}
	for (auto p : m)
		cnt = max(cnt, p.second);
	for (auto p : m)
	{
		if (p.second == cnt)
			cout << p.first;
	}

}