dh-winternagi 님의 블로그
(1181) 단어 정렬 본문
https://www.acmicpc.net/problem/1181
단계별로 풀어보기
13단계(정렬) 9번째
대부분의 언어에서 기본적으로 string는 사전 순 정렬로 대소 비교가 가능하므로 사전 순 정렬이 필요할 때 바로 string타입에 비교 연산자를 사용할 수 있다.
그리고 중복 제거하는 정렬을 너무 오랜만에 봐서 삽질을 했는데 정렬된 벡터에 unique를 이용해 중복을 지우고, 이때 원소의 개수가 줄어들 수 있으므로 출력할 때 이것을 고려해서 반복문의 범위를 설정해야 한다.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<string> v(n);
for(int i=0;i<n;i++) cin >> v[i];
sort(v.begin(), v.end(), [](string A, string B){
if(A.length()!=B.length()) return A.length()<B.length();
return A<B;
});
v.erase(unique(v.begin(), v.end()), v.end());
for(string elem:v) cout << elem << "\n";
return 0;
}'백준 (C++) > Solve' 카테고리의 다른 글
| (10815) 숫자 카드 (0) | 2026.04.14 |
|---|---|
| (10814) 나이순 정렬 (0) | 2026.04.14 |
| (11651) 좌표 정렬하기 2 (0) | 2026.04.14 |
| (11650) 좌표 정렬하기 (0) | 2026.04.13 |
| (1427) 소트인사이드 (0) | 2026.04.13 |
