문제 링크


🔷 분류

문자열, 정렬

✒️ 문제 설명

알파벳 소문자로 이루어진 N개의 단어가 들어오면 아래와 같은 조건에 따라 정렬하는 프로그램을 작성하시오.

  1. 길이가 짧은 것부터
  2. 길이가 같으면 사전 순으로

단, 중복된 단어는 하나만 남기고 제거해야 한다.

⬅️ 입력

첫째 줄에 단어의 개수 N이 주어진다. (1 ≤ N ≤ 20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.

➡️ 출력

조건에 따라 정렬하여 단어들을 출력한다.

💻 코드 (C++)

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

bool compare(string a, string b) {
	if (a.length() != b.length()) 
		return a.length() < b.length();
	else return a < b;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n;
	vector<string> v;
	string str;
	cin >> n;
	while(n--) {
		cin >> str;
		v.push_back(str);
	}
	sort(v.begin(), v.end(), compare);
	v.erase(unique(v.begin(), v.end()), v.end());
	for(string s : v) cout << s << "\n";
	return 0;
}

글 이동

Comments