[백준] 1339번 단어 수학

거북이·2023년 7월 14일
0

백준[골드4]

목록 보기
27/59
post-thumbnail

💡문제접근

  • 처음에는 무작위로 모든 경우를 탐색하는 무식한 방법만 떠올라서 어떻게 풀어야할지 고민이 많았다. 근데 최댓값을 출력하려면 결국 두 단어의 길이를 비교해서 각각의 자릿수를 매겨주면 큰 값이 할당된 문자부터 큰 숫자를 부여해주면 되겠다고 생각하여 딕셔너리와 람다 정렬을 적극적으로 활용하여 해결할 수 있었다.

💡코드(메모리 : 31256KB, 시간 : 44ms)

import sys
input = sys.stdin.readline

N = int(input())
my_dict = {}
words = []
for _ in range(N):
    words.append(input().strip())

for word in words:
    i = 1
    for t in word:
        if t not in my_dict:
            my_dict[t] = 10 ** (len(word) - i)
        else:
            my_dict[t] += 10 ** (len(word) - i)
        i += 1

temp = sorted(my_dict.items(), key=lambda x: x[1], reverse=True)
# 처음부터 dict()를 사용했는데 초기 변수명을 dict()로 설정해 Call을 할 수 없는 오류가 발생
# 그래서 변수명을 my_dict()로 바꿔주어 Call이 가능하도록 수정했더니 일사천리로 해결됨
sorted_dict = dict(temp)

number_list = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
for i in range(len(sorted_dict)):
    word, count = temp[i]
    val = count * number_list[i]
    sorted_dict[word] = val

ans = 0
for i in sorted_dict.values():
    ans += i
print(ans)

💡소요시간 : 38m

0개의 댓글