study gomi

[백준/baekjoon] 20920번 영단어 암기는 괴로워 (파이썬) 본문

Practice/Baekjoon

[백준/baekjoon] 20920번 영단어 암기는 괴로워 (파이썬)

공부하곰 2024. 1. 30. 00:46
728x90
반응형

내 제출

- 네 번을 틀렸다. 그것도 다 같은 이유(시간 초과)로...

- 딕셔너리로 풀었다(dict 참고 : https://taetaegom.tistory.com/18)

import sys

n, m = map(int, sys.stdin.readline().rstrip().split())
word_dict = {}

for _ in range(n):
    word = sys.stdin.readline().rstrip()

    if len(word) < m:
        continue
    else:
        if word in word_dict:
            word_dict[word] += 1
        else:
            word_dict[word] = 1

word_dict = sorted(word_dict.items(), key=lambda x: (-x[1], -len(x[0]), x[0]))

for i in word_dict:
    print(i[0])

 

오답노트

- dictionary를 sorted로 정렬하면 list가 된다. 그래서 그 뒤로 반복문 사용할 때 items나 key로 접근 x

- 이거 풀고 곧바로 다른 비슷한 걸 풀었는데 비슷한 이유로 틀렸다.

# 앞 뒤 코드는 생략했음.

# dict를 정렬하여 리스트로 변환
sorted_metals = sorted(metal_dict.items(), key=lambda x: (-x[1]))

for weight, value in sorted_metals:

 

- 람다식 이용해서 정렬하는 방법 참고

# 딕셔너리의 키값들을 매개변수 x에 넣어 x로 정렬
sorted(dict.keys(), key=lambda x: x)

# 딕셔너리의 value값들을 매개변수 x에 넣어 x로 정렬
sorted(dict.values(), key=lambda x: x)

# 딕셔너리의 키값들을 매개변수 x에 넣어 dict[x](dict의 value값) 정렬
sorted(dict, key=lambda x: dict[x])

# 딕셔너리의 키,값 쌍들을 매개변수 x에 넣어 키값 정렬
sorted(dict.items(), key=lambda x: x[0])

 

결과

- 두 번째 틀렸습니다는 defulatdict를 사용했는데 왜 틀린지 모르겠다. 반례 모르겠ㄷr...ㅎ

- 그래서 다시 첨에 시간 초과 났던 문제로 돌아가서 풀어보는데 계속 안 됨.

- input을 sys.stdin 사용해줬더니 해결됐다. 허무...

 

문제

https://www.acmicpc.net/problem/20920

728x90
반응형