알고리즘
백준 20920: 영단어 암기는 괴로워 Python
짱이 되었어
2022. 11. 29. 10:51
https://www.acmicpc.net/problem/20920
20920번: 영단어 암기는 괴로워
첫째 줄에는 영어 지문에 나오는 단어의 개수 $N$과 외울 단어의 길이 기준이 되는 $M$이 공백으로 구분되어 주어진다. ($1 \leq N \leq 100\,000$, $1 \leq M \leq 10$) 둘째 줄부터 $N+1$번째 줄까지 외울 단
www.acmicpc.net
import sys
input = sys.stdin.readline
N, M = map(int, input().split()) # N: 단어 개수, M: 길이 기준
words = {} # result = {'apple': [5, 1, 'apple'],}
for i in range(N):
word = input().strip()
if len(word) < M:
continue
else:
if words.get(word):
words[word][1] += 1
else:
words[word] = [len(word), 1, word]
result = sorted(words.items(), key=lambda x: (-x[1][1], -x[1][0], x[1][2]))
for r in result:
print(r[0])