Hi

[백준/Python] 20920. 영단어 암기는 괴로워 본문

Algorithm/Baekjoon

[백준/Python] 20920. 영단어 암기는 괴로워

seungminleeee 2025. 1. 14. 13:10

길이가 M 이상인 단어들의 수를 세서 조건에 맞게 정렬 하려고 했다.

조건
1. 자주 나오는 단어일수록 앞에 배치한다. => 개수 내림차순
2. 해당 단어의 길이가 길수록 앞에 배치한다. => 길이 내림차순
3. 알파벳 사전 순으로 앞에 있는 단어일수록 앞에 배치한다 => 오름차순

 

계속 시간초과가 나서 input을 전부 한줄로 받은 다음 슬라이싱 했다..

 

import sys
from collections import Counter
input = sys.stdin.read
data = input().splitlines()

# print(data)
# ['7 4', 'apple', 'ant', 'sand', 'apple', 'append', 'sand', 'sand']

N, M = map(int, data[0].split())
words = data[1:]

words_list = [word for word in words if len(word) >= M]

words_cnt = Counter(words_list)
words_set = list(words_cnt.items())

words_set.sort(key=lambda x: (-x[1], -len(x[0]), x[0]))

for word, cnt in words_set:
    print(word)