-
[알고리즘][백준] 20920. 영단어 암기는 괴로워알고리즘 2024. 4. 17. 02:43반응형
문제
https://www.acmicpc.net/problem/20920
# 자주 나오는 단어일수록 앞에 배치한다. # 해당 단어의 길이가 길수록 앞에 배치한다. # 알파벳 사전 순으로 앞에 있는 단어일수록 앞에 배치한다 import sys input = sys.stdin.readline N, M = map(int, input().rstrip().split()) words = {} for _ in range(N): word = input().rstrip() if (len(word) < M): continue words[word] = 1 if word not in words else words[word] + 1 words = sorted(words.items(), key = lambda x : (-x[1], -len(x[0]), x[0])) for word in words: print(word[0])
sorted(words.items(), key = lambda x : (-x[1], -len(x[0]), x[0]))
sorted 메서드에 key = labmda x를 넣으면, x를 기준으로 원하는 정렬 기준으로 정렬할 수 있습니다. 위의 경우 x는 dict의 element이기 때문에 x[0] = key (단어), x[1] = value (빈도 수) 가 됩니다.반응형'알고리즘' 카테고리의 다른 글
[알고리즘][백준] 11659. 구간 합 구하기 4 (1) 2024.04.27 [알고리즘][백준] 2607. 비슷한 단어 (0) 2024.04.25 [알고리즘][백준] 14719. 빗물 (0) 2024.04.22 [알고리즘][백준] 2493. 탑 (1) 2024.04.20 [알고리즘][백준] 9017. 크로스 컨트리 (0) 2024.04.18