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

+ Recent posts