병합정렬을 구현하고, 그 기준을 문제의 정렬 기준으로 설정하였음
import sys
input = sys.stdin.readline
n = int(input())
ls = [input().strip() for _ in range(n)]
def merge_sort(l):
if len(l) == 1:
return l
m = len(l) // 2
left = merge_sort(l[:m])
right = merge_sort(l[m:])
return merge(left, right)
def merge(left, right):
l = []
i, j = 0, 0
while i < len(left) and j < len(right):
if len(left[i]) < len(right[j]) or (len(left[i]) == len(right[j]) and left[i] < right[j]):
l.append(left[i])
i += 1
else:
l.append(right[j])
j += 1
if i < len(left):
l.extend(left[i:])
elif j < len(right):
l.extend(right[j:])
return l
ls = list(set(ls))
for i in merge_sort(ls):
print(i)