728x90
구글링을 통해 각 자릿수의 합을 기준으로 정렬하면 되는 것을 알고 풀었다. dict 를 이용했는데 헷갈리는 부분이 있었다. key와 value 를 한번에 받기 위해서는 items 를 이용해야 한다./
import sys
from collections import deque
from itertools import permutations
import string
import copy
num = int(input())
lists = [ ]
for i in range(0,num):
lists.append(list(sys.stdin.readline().rstrip()))
lists = list(sorted(lists,key = lambda x : -len(x)))
alpha_temp = list(string.ascii_uppercase)
# 사용된 알파벳
alpha = { }
for i in range(0,len(lists)):
for j in range(0,len(lists[i])):
if lists[i][j] not in alpha:
alpha[lists[i][j]] = 0
index = 9
for i in range(0,len(lists)):
for j in range(0,len(lists[i])):
alpha[lists[i][j]] += 10**(len(lists[i])-j-1)
a = [ ]
for i,j in alpha.items():
a.append([i,j])
a = list(sorted(a,key = lambda x : -x[1]))
index = 9
for i in range(0,len(a)):
for j in range(0,len(lists)):
for z in range(0,len(lists[j])):
if lists[j][z] == a[i][0]:
lists[j][z] = index
index -= 1
answer = 0
for i in range(0,len(lists)):
temp = ''
for j in range(0,len(lists[i])):
temp += str(lists[i][j])
answer += int(temp)
print(answer)
728x90
'🟢 알고리즘 문제 풀이 > Baekjoon' 카테고리의 다른 글
| [파이썬] 백준 10799 쇠막대기 (0) | 2023.05.28 |
|---|---|
| [파이썬] 백준 13975 파일 합치기 3 (0) | 2023.05.25 |
| [파이썬] 백준 16432 떡장수와 호랑이 (0) | 2023.05.22 |
| [파이썬] 백준 11404 플로이드 (0) | 2023.05.18 |
| [파이썬] 백준 1563 개근상 (0) | 2023.05.16 |