-> unordered_map으로 받아서 정렬까지 다 하려고 했는데, map, set은 키로 정렬이 가능하지 second로는 정렬이 불가능하다. 그래서 vector로 옮긴 뒤에 정렬하였다.
정렬 함수를 만들어서 우선순위대로 하였다. string은 비교시 더 작은 것이 사전순으로 앞 숫자기 때문에 기호의 방향에 주의하자. (아스키 코드로 생각하면 편하다)
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
unordered_map<string, int> dictionary;
bool compairVec(const pair<string, int>& A, const pair<string, int>& B)
{
if (A.second != B.second)
{
return A.second > B.second;
}
else
{
if (A.first.size() != B.first.size())
{
return A.first.size() > B.first.size();
}
else
{
return A.first < B.first;
}
}
}
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N{}, M{}, K{};
cin >> N >> M;
string s;
for (int i = 0; i < N; i++)
{
cin >> s;
if (s.size() >= M)
{
dictionary[s]++;
}
}
vector< pair<string, int>> Vec(dictionary.begin(), dictionary.end());
sort(Vec.begin(), Vec.end(), compairVec);
for (int i = 0; i < Vec.size(); i++)
{
cout << Vec[i].first << '\n';
}
}
알게 된 점: (알았는데 까먹은 것 같지만) 벡터에 다른 컨테이너의 begin~end를 넣으면 바로 붙여넣기가 됨
'C++_코딩테스트 > 컨테이너' 카테고리의 다른 글
[백준 1927번/C++] 최소 힙 (0) | 2024.02.05 |
---|