Coding Test
[백준] 단어 수학
상상쓰
2021. 6. 6. 12:56
https://www.acmicpc.net/problem/1339
1339번: 단어 수학
첫째 줄에 단어의 개수 N(1 ≤ N ≤ 10)이 주어진다. 둘째 줄부터 N개의 줄에 단어가 한 줄에 하나씩 주어진다. 단어는 알파벳 대문자로만 이루어져있다. 모든 단어에 포함되어 있는 알파벳은 최대
www.acmicpc.net
주어진 단어에 index 마다 가중치를 부여한다. 같은 index 라도 중복이 일어날 때는 같은 index 의 다른 알파벳보다 가중치를 크게 줘야 하므로 index 의 초기 가중치를 10 ** j 로 한다.
import sys
from collections import defaultdict
N = int(sys.stdin.readline())
dic = defaultdict(int)
array = []
for i in range(N):
s = sys.stdin.readline().strip()
array.append(s)
s = s[::-1]
for j in range(0, len(s)):
dic[s[j]] = dic[s[j]] + (10 ** j)
c = 9
answer = 0
for key, value in sorted(dic.items(), key = lambda x : -x[1]):
dic[key] = c
c = c - 1
for i in array:
string = ''
for j in i:
string = string + str(dic[j])
answer = answer + int(string)
print(answer)