본문 바로가기
Algorithm/BOJ

[백준/BOJ] 1181번 - 단어 정렬 (C++)

by shine-jung 2022. 3. 21.
반응형

문제 링크


코드

#include <bits/stdc++.h>
using namespace std;

bool comp(string a, string b) {
    if (a.length() != b.length())
        return a.length() < b.length();
    return a < b;
}

int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    set<string, decltype(comp)*> dict(comp);
    string s;
    while (n--) {
        cin >> s;
        dict.insert(s);
    }
    for (auto i : dict)
        cout << i << '\n';
}



설명


https://stackoverflow.com/questions/2620862/using-custom-stdset-comparator

위 링크의 3번째 솔루션을 참고하였다.

set에 정렬을 하면서 값을 추가할 수 있다.




(주의) 기록용으로 작성한 글입니다. 좋은 코드가 아닐 수 있습니다.

댓글 환영합니다!


반응형

댓글