# 생각
컨테이너 map을 이용하면 자동으로 중복을 제거하고 정렬을 처리할 수 있다
또는 vector를 이용하여 sort함수의 람다식을 사용하면 정렬을 처리할 수 있다
# 전체 코드
#include <bits/stdc++.h>
using namespace std;
// map을 이용한 풀이
void solution1()
{
int n;
map<string, int> m[55];
cin >> n;
for (int i = 0; i < n; i++)
{
string s;
cin >> s;
m[s.length()].insert({ s, 0 });
}
for (int i = 0; i < 55; i++)
{
if (m[i].empty()) continue;
for (auto iter = m[i].begin(); iter != m[i].end(); iter++)
{
cout << iter->first << '\n';
}
}
}
// vector를 이용한 풀이
void solution2()
{
int n;
cin >> n;
vector<string> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
sort(v.begin(), v.end(), [](const string& lhs, const string& rhs)
{
if (lhs.length() != rhs.length()) return lhs.length() < rhs.length(); // 길이가 다르면 길이순
return lhs < rhs; // 길이가 같으면 사전순
});
v.erase(unique(v.begin(), v.end()), v.end()); // 중복 원소 제거
for (string& s : v) cout << s << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
//solution1();
solution2();
}
https://www.acmicpc.net/problem/1181
1181번: 단어 정렬
첫째 줄에 단어의 개수 N이 주어진다. (1 ≤ N ≤ 20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.
www.acmicpc.net
'Algorithm > BOJ' 카테고리의 다른 글
[BOJ] 11656_접미사 배열 with cpp (0) | 2022.03.01 |
---|---|
[BOJ] 2910_빈도 정렬 with cpp (0) | 2022.03.01 |
[BOJ] 2751_수 정렬하기 2 with cpp (0) | 2022.02.28 |
[BOJ] 2750_수 정렬하기 with cpp (0) | 2022.02.28 |
[BOJ] 18808_스티커 붙이기 with cpp (0) | 2022.02.21 |