Created
January 6, 2019 12:08
[BOJ] 1181. 단어 정렬 : https://www.acmicpc.net/problem/1181
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<string> | |
#include<algorithm> | |
#include<set> | |
using namespace std; | |
struct compare | |
{ | |
bool operator()(const string& a, const string& b) { | |
if (a.size() != b.size()) | |
return a.size() < b.size(); | |
return a < b; | |
} | |
}; | |
int main() { | |
int n; | |
cin >> n; | |
set<string, compare> s; | |
string tmp; | |
for (int i = 0; i < n; i++) { | |
cin >> tmp; | |
s.insert(tmp); | |
} | |
for (set<string, compare>::iterator it = s.begin(); it != s.end(); it++) { | |
cout << *it << endl; | |
} | |
return 0; | |
} |
set<pair<int, string>>를 이용하는 게 더 나을 듯 함다.
깔끔하네요*-*!!
set<pair<int, string>>를 이용하는 게 더 나을 듯 함다.
pair 이용하면 뭐가더 좋을까요?
set<pair<int, string>>를 이용하는 게 더 나을 듯 함다.
pair 이용하면 뭐가더 좋을까요?
set은 자동 정렬해주는 자료구조 중 하나입니다.
pair<int,string>을 set의 자료형으로 사용하는 경우 first에 해당하는 int가 우선적으로 판단되어 정렬기준으로 사용되고
int가 같은 경우 second에 해당하는 string이 이후 정렬기준으로 사용됩니다.
따라서 pair<문자열 길이, 문자열> 순으로 set에 저장을 한다면 별도의 operator 정의 없이 기존의 set으로만 문제를 풀 수 있습니다. :)
@fpdjsns
set, pair 편리한 자료구조군요. 답변감사합니다:D
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
멋져요~