반응형
문제 링크
코드
#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에 정렬을 하면서 값을 추가할 수 있다.
(주의) 기록용으로 작성한 글입니다. 좋은 코드가 아닐 수 있습니다.
댓글 환영합니다!
반응형
'Algorithm > BOJ' 카테고리의 다른 글
[백준/BOJ] 1267번 - 핸드폰 요금 (C++) (0) | 2022.03.21 |
---|---|
[백준/BOJ] 1260번 - DFS와 BFS (C++) (0) | 2022.03.21 |
[백준/BOJ] 1159번 - 농구 경기 (C++) (0) | 2022.03.21 |
[백준/BOJ] 1157번 - 단어 공부 (C++) (0) | 2022.03.21 |
[백준/BOJ] 1065번 - 한수 (C++) (0) | 2022.03.21 |
댓글