PS

BOJ 20920 : 영단어 암기는 괴로워

lickelon 2024. 2. 10. 22:17
  • 문제 링크 : boj.kr/20920
  • 난이도 : S3
  • 태그 : 문자열, 정렬
 

20920번: 영단어 암기는 괴로워

첫째 줄에는 영어 지문에 나오는 단어의 개수 $N$과 외울 단어의 길이 기준이 되는 $M$이 공백으로 구분되어 주어진다. ($1 \leq N \leq 100\,000$, $1 \leq M \leq 10$) 둘째 줄부터 $N+1$번째 줄까지 외울 단

www.acmicpc.net


코드

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()

#define INF 0x7FFFFFFF

using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;

bool comp(pair<string, int> &a, pair<string, int> &b) {
    if(a.second == b.second) {
        if(a.first.length() == b.first.length()) {
            return a.first < b.first;
        }
        return a.first.length() > b.first.length();
    }
    return a.second > b.second;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n, m;
    cin >> n >> m;
    map<string, int> _m;
    for(int i = 0; i < n; i++) {
        string s;
        cin >> s;
        if(s.length() < m) continue;
        _m[s]++;
    }
    vector<pair<string, int>> arr(all(_m));
    sort(all(arr), comp);

    for(auto u : arr) {
        cout << u.first << "\n";
    }

    return 0;
}

풀이

영어 지문에 등장한 단어를 세기 위해 map을 사용한다.

map에 저장된 데이터를 정렬하기 위해 vector로 옮긴다.

조건에 맞는 정렬 함수를 이용하여 정렬한 뒤, 단어를 출력한다.

'PS' 카테고리의 다른 글

BOJ 5052 : 전화번호 목록  (1) 2024.02.12
BOJ 20044 : Project Teams  (0) 2024.02.11
BOJ 2986 : 파스칼  (0) 2024.02.09
BOJ 9575 : 행운의 수  (1) 2024.02.08
BOJ 25344 : 행성 정렬  (2) 2024.02.07