main
[백준 - 1181번] 단어 정렬 본문
- set 사용
- set 정렬 기준 변경 -> 구조체 써서 바꿀 수 있음
- 정의 할 때 set<자료형, 기준> 이렇게 하면됨
#include <iostream>
#include <string>
#include <set>
using namespace std;
struct Compare {
bool operator() (const string& a, const string& b) const {
// 같은 경우는 less정렬인가 봄(불확실)
if (a.size() == b.size())
return a < b;
else
return a.size() < b.size();
}
};
int main(void)
{
int N;
cin >> N;
string str;
set<string, Compare> s;
for (int i = 0; i < N; i++)
{
cin >> str;
s.insert(str);
}
for (set <string>::iterator it = s.begin(); it !=s.end(); ++it)
{
cout << *it << '\n';
}
return 0;
}
[참고 자료]
728x90
'Algorithm' 카테고리의 다른 글
[C++ 공부] 7. 영어단어 복구 (0) | 2022.01.30 |
---|---|
[백준 - 2935번] 소음 (0) | 2022.01.26 |
[백준 - 10828번] 스택 (0) | 2022.01.23 |
[백준 - 18111번] 마인크래프트 (0) | 2022.01.23 |
[백준 - 1085번] 직사각형에서 탈출 (0) | 2022.01.23 |
Comments