Notice
Recent Posts
Recent Comments
Link
«   2026/06   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
관리 메뉴

dh-winternagi 님의 블로그

(20920) 영단어 암기는 괴로워 본문

백준 (C++)/Solve

(20920) 영단어 암기는 괴로워

dh-winternagi 2026. 4. 16. 08:22

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

단계별로 풀어보기

18단계(심화 2) 5번째

 

 

 

문제에서 설명한 정렬 조건을 따르는 커스텀 비교 함수를 만들면 된다.

단어 등장 빈도가 조건 중 하나이므로 이 부분은 map을 이용하면 된다.

 

 

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

int main() 
{
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  
  int n, m;
  
  cin >> n >> m;

  map<string, int> mp;
  
  while(n--){
    string word;
    
    cin >> word;
    
    if(word.length()<m)  continue;
    
    mp[word]++;
  }
  
  vector<pair<string,int>> v(mp.begin(), mp.end());
  
  sort(v.begin(), v.end(), [](auto A, auto B){
    if(A.second!=B.second)  return A.second>B.second;
    if(A.first.length()!=B.first.length())  return A.first.length()>B.first.length();
    return A<B;
  });
  
  for(auto elem:v){
    cout << elem.first << "\n";
  }
  
  return 0;
}

'백준 (C++) > Solve' 카테고리의 다른 글

(10870) 피보나치 수 5  (0) 2026.04.16
(27433) 팩토리얼 2  (0) 2026.04.16
(2108) 통계학  (0) 2026.04.16
(26069) 붙임성 좋은 총총이  (0) 2026.04.16
(25192) 인사성 밝은 곰곰이  (0) 2026.04.16