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

#문제

백준 1181번 단어 정렬

#풀이

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

string arr[20001];

int cmp(string a, string b)
{
	if (a.length() == b.length())
	{
		return a < b;
	}
	else
	{
		return a.length() < b.length();
	}
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(NULL);

	int N;
	cin >> N;

	for (int i = 0; i < N; ++i)
	{
		cin >> arr[i];
	}

	sort(arr, arr + N, cmp);

	for (int i = 0; i < N; ++i)
	{
		if (arr[i] == arr[i - 1])
		{
			continue;
		}
		else
		{
			cout << arr[i] << '\n';
		}
	}

	return 0;
}

#정리

알고리즘 라이브러리 내 sort 메서드를 사용했다. 사용자 정의 비교 함수 Compare를 정의하여 문제의 규칙에 따라 입력받은 문자를 정렬 후, 출력 시 중복된 단어가 출력되지 않도록 코드를 작성하여 해결했다.




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • [백준] 2309번 일곱 난쟁이 / C++
  • [백준] 11651번 좌표 정렬하기 2 / C++
  • [백준] 10814번 나이순 정렬 / C++
  • [백준] 1940번 주몽 / C++
  • [백준] 25206번 너의 평점은 / C++