https://www.acmicpc.net/problem/1181
알파벳 소문자로 이루어진 N개의 단어가 들어오면 아래와 같은 조건에 따라 정렬하는 프로그램을 작성하시오.
길이가 짧은 것부터
길이가 같으면 사전 순으로
첫째 줄에 단어의 개수 N이 주어진다. (1 ≤ N ≤ 20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(string a, string b) {
if (a.length() == b.length()) {
return a < b;
}
else return a.length() < b.length();
}
int main()
{
cin.tie(0);
cout.tie(0);
cin.sync_with_stdio(0);
int n;
cin >> n;
vector<string> arr;
string tmp;
for (int i = 0; i < n; i++) {
cin >> tmp;
if (find(arr.begin(), arr.end(),tmp) == arr.end()) {
arr.push_back(tmp);
}
}
sort(arr.begin(), arr.end(), compare);
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << "\n";
}
return 0;
}