입출력
풀이
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int cnt;
cin >> cnt;
string word;
int key;
vector<pair<int, string>> words;
for (int i = 0; i < cnt; i++)
{
cin >> word;
key = word.length();
words.push_back(make_pair(key, word));
}
sort(words.begin(), words.end());
words.erase(unique(words.begin(), words.end()), words.end());
for (int i = 0; i < words.size(); i++)
{
cout << words[i].second << endl;
}
}
sort함수는 2차원 벡터의 정렬에서 첫번째 원소를 먼저 정렬한 후, 두번째 원소를 정렬한다
따라서 2차원 벡터 vector<pair<int, string>> words의 첫번째 원소에 word.length()를 넣어주고,
두번째에 word를 넣어주면 우선적으로 단어가 짧은 순, 단어의 길이가 같다면 두번째로 문자정렬을 해준다
다만 중복제거를 위해서 unique함수를 이용하여 중복된 값들을 쓰레기값으로 보내고, erase함수로 쓰레기값들은 다 날려버린다.
unique함수는 정렬을 마친 뒤 쓰레기값들의 첫번째 위치를 반환하므로 거기서부터 erase를 바로 해주면 깔끔하게 제거된다
'> 코딩테스트' 카테고리의 다른 글
[백준] 10815 숫자 카드 (C++) (1) | 2023.06.11 |
---|---|
[백준] 10814 나이순 정렬 (C++) (0) | 2023.06.07 |
[백준] 11651 좌표 정렬하기2 (C++) (0) | 2023.06.06 |
[백준] 11650 좌표 정렬하기 (C++) (0) | 2023.06.04 |
[백준] 10989 수 정렬하기 (C++) (0) | 2023.06.04 |