728x90
Key Point
1. sort 알고리즘과 vector의 unique 기능을 사용하자.
2. sort 알고리즘의 compare의 return 부등호를 조건에 맞게 수정하자.
( 길이가 같으면 사전순 정렬, 길이가 다르면 크기별 정렬)
3.sort를 마무리 한후, 겹치는 문자 제거하고 출력
알게 된 문법
1. algorithm lib에 sort를 사용하는 법.
어떤 기준으로 정렬을 실행할 때 sort(시작점 ,종료점, 기준)
2. vector의 겹치는 요소를 지우는 방법
v.erase(unique(v.begin(), v.end()), v.end());
3. 반복문을 간단하게 선언하는 방법
for(string i : v) // v에서 뽑아낸 i를 가져올 수 있음
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool compare(string a, string b) {
if (a.size() != b.size())
return a.size() < b.size();
else
return a < b;
}
int main() {
int N;
string str;
cin >> N;
vector<string>v;
for (int i = 0; i < N; i++) {
cin >> str;
v.push_back(str);
}
sort(v.begin(), v.end(), compare); //compare 기준으로 sort
v.erase(unique(v.begin(), v.end()), v.end());//겹치는 문자 제거
for (string i : v)
cout << i << '\n';
return 0;
}
728x90
'백준 풀이' 카테고리의 다른 글
[백준/BOJ] - 1436번 c++ 풀이 (0) | 2022.11.12 |
---|---|
[백준/BOJ] - 1259번 c++ 풀이 (0) | 2022.11.12 |
[백준/BOJ] - 1018번 c++ 풀이 - 첫 실버 (0) | 2022.11.11 |
[백준/BOJ] - 11720번 c++ 풀이 (0) | 2022.11.11 |
[백준/BOJ] - 3052번 c++ 풀이 (0) | 2022.11.11 |