BOJ - 1339 - 단어 수학

Updated:

import sys

number = [
    [0,"A"],[0,"B"],[0,"C"],[0,"D"],[0,"E"],
    [0,"F"],[0,"G"],[0,"H"],[0,"I"],[0,"J"],
    [0,"K"],[0,"L"],[0,"M"],[0,"N"],[0,"O"],
    [0,"P"],[0,"Q"],[0,"R"],[0,"S"],[0,"T"],
    [0,"U"],[0,"V"],[0,"W"],[0,"X"],[0,"Y"],
    [0,"Z"],
]

def solution():
    N = int(sys.stdin.readline())

    arr = [0 for _ in range(26)]
    for i in range(N):
        word = sys.stdin.readline().strip()
        for j in range(len(word)):
            index = ord(word[len(word) - 1 - j]) - 65
            if j == len(word) - 1:
                arr[index] = 1
            number[index][0] = number[index][0] + 10 ** j

    number.sort(reverse=True)
    if number[9][0] != 0:
        for i in range(9, -1, -1):
            if arr[ord(number[i][1]) - 65] == 0:
                temp = list(number[i])
                number.remove(temp)
                number.append(temp)
                break

    ret = 0
    for i in range(10):
        ret += number[i][0] * (9 - i)
    print(ret)

solution()

https://www.acmicpc.net/problem/1339

Categories:

Updated:

Leave a comment