Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Created January 6, 2019 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/65ecf5c52cb88b6f1b787161b7b3814c to your computer and use it in GitHub Desktop.
Save fpdjsns/65ecf5c52cb88b6f1b787161b7b3814c to your computer and use it in GitHub Desktop.
[BOJ] 1181. 단어 정렬 : https://www.acmicpc.net/problem/1181
#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;
}
@vinus322
Copy link

멋져요~

@vinus322
Copy link

set<pair<int, string>>를 이용하는 게 더 나을 듯 함다.

@Yimyujin
Copy link

깔끔하네요*-*!!

@Yimyujin
Copy link

Yimyujin commented Jan 12, 2019

@vinus322

set<pair<int, string>>를 이용하는 게 더 나을 듯 함다.
pair 이용하면 뭐가더 좋을까요?

@fpdjsns
Copy link
Author

fpdjsns commented Jan 12, 2019

@Yimyujin

set<pair<int, string>>를 이용하는 게 더 나을 듯 함다.
pair 이용하면 뭐가더 좋을까요?

set은 자동 정렬해주는 자료구조 중 하나입니다.
pair<int,string>을 set의 자료형으로 사용하는 경우 first에 해당하는 int가 우선적으로 판단되어 정렬기준으로 사용되고
int가 같은 경우 second에 해당하는 string이 이후 정렬기준으로 사용됩니다.
따라서 pair<문자열 길이, 문자열> 순으로 set에 저장을 한다면 별도의 operator 정의 없이 기존의 set으로만 문제를 풀 수 있습니다. :)

@Yimyujin
Copy link

@fpdjsns
set, pair 편리한 자료구조군요. 답변감사합니다:D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment