[백준 1181] 단어 정렬 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
입력으로 주어지는 단어들을 두 가지 기준으로 정렬하는 문제입니다. 
기준은 다음과 같습니다.
- 단어의 길이가 짧은 것이 앞 순서로
- 단어의 길이가 같다면 알파벳 사전 순으로 
Code
[ C# ] 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
namespace Solution {
  class Program {
    static void Main(string[] args) {
      var n = int.Parse(Console.ReadLine()!);
      var words = new string[n];
      for (int i = 0; i < n; i++)
        words[i] = Console.ReadLine()!;
      words = words
            .Distinct()
            .OrderBy(w => w.Length)
            .ThenBy(w => w)
            .ToArray();
      foreach (var word in words)
        Console.WriteLine(word);
    }
  }
}
[ C++ ] 
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
#include <bits/stdc++.h>
using namespace std;
bool comp(const string& a, const string& b) {
  if (a.size() != b.size())
    return a.size() < b.size();
  return a < b;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n; cin >> n;
  vector<string> words(n);
  for (int i = 0; i < n; i++)
    cin >> words[i];
  sort(words.begin(), words.end(), comp);
  words.erase(unique(words.begin(), words.end()), words.end());
  for (const auto& word : words)
    cout << word << "\n";
  return 0;
}