Hi
[백준/Python] 20920. 영단어 암기는 괴로워 본문
길이가 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)'Algorithm > Baekjoon' 카테고리의 다른 글
| [백준/Python] 11725. 트리의 부모 찾기 (0) | 2025.03.27 |
|---|---|
| [백준/Python] 19949. 영재의 시험 (0) | 2025.03.15 |
| [백준/Python] 1149. RGB 거리 (2) | 2025.03.12 |
| [백준/Python]11048. 이동하기 (0) | 2025.03.03 |
| [백준/Python] 1213. 팰린드롬 만들기 (5) | 2025.01.03 |