- 문제
- 풀이
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
bool compare(string str1, string str2)
{
// 길이가 같은 경우 사전 순으로 정렬하고
if (str1.size() == str2.size())
{
return str1 < str2;
}
// 길이가 다른 경우 짧은 순으로 정렬한다.
else
{
return str1.size() < str2.size();
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n = 0;
cin >> n;
vector<string> str;
string input;
for (int i = 0; i < n; i++)
{
cin >> input;
str.push_back(input);
}
sort(str.begin(), str.end(), compare);
str.erase(unique(str.begin(), str.end()), str.end());
vector<string>::iterator iter;
for (iter = str.begin(); iter != str.end(); ++iter)
{
cout << *iter << '\n';
}
return 0;
}
- 메모
중복을 삭제하기 위해서 unique를 사용해서 erase 해주었다.
이전엔 list에서 unique를 사용하는 법에 대해서만 알고 있었는데 vector에서도 이런 방식으로 간단하게 중복되는 원소를 제거할 수 있다는 걸 알게 되었다.
'코딩테스트' 카테고리의 다른 글
★ [백준/11050번/C++] 이항 계수 1 (0) | 2024.07.12 |
---|---|
[백준/2798번/C++] 블랙잭 (0) | 2024.07.08 |
[백준/10814번/C++] 나이순 정렬 (0) | 2024.06.30 |
[백준/11651번/C++] 좌표 정렬하기 2 (0) | 2024.06.28 |
[백준/11650번/C++] 좌표 정렬하기 (0) | 2024.06.26 |