Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Archives
Today
Total
관리 메뉴

코딩로그

[백준/BOJ/C++] 1181번 단어 정렬 본문

YJ/C++

[백준/BOJ/C++] 1181번 단어 정렬

Team DAON 2021. 12. 27. 14:52

[문제]

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

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

[입력]

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

[출력]

조건에 따라 정렬하여 단어들을 출력한다. 단, 같은 단어가 여러 번 입력된 경우에는 한 번씩만 출력한다.

[Source Code]

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

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

int main() {
	int test;
	cin >> test;

	string *arr = new string[test];
	for(int i = 0; i < test; i++){
		string input;
		cin >> input;
		arr[i] = input;
	}

	sort(arr, arr+test, change);
	cout << arr[0] << "\n";
	for(int i = 1; i < test; i++){
		if(arr[i-1] != arr[i])
			cout << arr[i] << "\n";
	}
}

[결과 화면]