BOJ/C++
BOJ [C++])1181번 단어 정렬
도리닥닥
2021. 12. 19. 17:21
728x90
https://www.acmicpc.net/problem/1181
1181번: 단어 정렬
첫째 줄에 단어의 개수 N이 주어진다. (1 ≤ N ≤ 20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.
www.acmicpc.net
1. 문제
- 알파벳 소문자로 이루어진 N개의 단어가 들어오면 아래와 같은 조건에 따라 정렬하는 프로그램을 작성하시오.
1. 길이가 짧은 것부터
2. 길이가 같으면 사전 순으로
2. PS
- <algorithm> 라이브러리의 sort, stable sort를 사용하여 해결
- 같은 문자는 unique 함수를 이용하여 해결
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
using namespace std;
int main() {
vector<string> w;
string s;
int n;
cin >> n;
for(int i = 0; i < n; i++) {
cin >> s;
w.push_back(s);
}
sort(w.begin(), w.end());
stable_sort(w.begin(), w.end(), [](string s1, string s2){
return (s1.size() < s2. size());
});
auto last = unique(w.begin(), w.end());
w.erase(last, w.end());
for(auto x : w) cout << x << endl;
}