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