알파벳 소문자로 이루어진 단어들을 입력받으면 길이 순으로, 길이가 같으면 사전 순으로 정렬하는 문제
이거야 말로 벡터를 쓰면 좋은 문제다 (11650, 11651 참고)
벡터는 기본 first 기준으로, 같다면 second 기준으로 정렬하기 때문
그러므로 first엔 단어의 길이를, second엔 단어를 넣어주고 정렬하면 된다
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
|
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<pair<int, string>> arr(n); //첫번째는 길이 두번째는 문자
for (int i = 0; i < n; i++) {
cin >> arr[i].second;
arr[i].first = arr[i].second.length();
}
sort(arr.begin(), arr.end()); //길이 같으면 자동으로 second 기준!!
cout << arr[0].second << "\n";
for (int j = 1; j < n; j++) {
if(arr[j].second != arr[j-1].second)
cout << arr[j].second << "\n";
}
}
|
cs |
벡터만 쓸 줄 안다면 쉬운 문제
'Algorithm > BOJ' 카테고리의 다른 글
[백준 알고리즘/BOJ/C++] 15649 N과 M (1) (0) | 2021.01.30 |
---|---|
[백준 알고리즘/BOJ/C++] 10814 나이순 정렬 (0) | 2021.01.26 |
[백준 알고리즘/BOJ/C++] 11650, 11651 좌표 정렬하기 (0) | 2021.01.26 |
[백준 알고리즘/BOJ/C++] 1427 소트인사이드 (0) | 2021.01.26 |
[백준 알고리즘/BOJ/C++] 2108 통계학 (0) | 2021.01.26 |