Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
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
Tags
more
Archives
Today
Total
관리 메뉴

main

[백준 - 1181번] 단어 정렬 본문

Algorithm

[백준 - 1181번] 단어 정렬

1984 2022. 1. 24. 03:20
  • 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;
}

 

[참고 자료]

https://chanhuiseok.github.io/posts/algo-46/

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