[BOJ] 1339

12760 단어 알고리즘DPTDPT

[BOJ]

me

자리가 앞일 수록 큰 것을 넣어줬다..

import sys
input=sys.stdin.readline

n=int(input())
words=[input().strip() for _ in range(n)]

words.sort(key=lambda x:len(x),reverse=True)
alphabets={}
num=9
form=''

for i in range(1,n):
    for j in range(len(words[0])-len(words[i])):
        words[i]='-'+words[i]
        
for i in range(len(words[0])):
    for j in range(n):
        if words[j][i] == '-': continue
        if words[j][i] not in alphabets:
            alphabets[words[j][i]]=str(num)
            num-=1
            
for word in words:
    for w in word:
        if w=='-':continue
        form+=alphabets[w]
    form+='+'
    
print(eval(form[:-1]))

solution

자릿수를 계산해서 순서대로 9부터 할당
참고

  • me
import sys
input=sys.stdin.readline

n=int(input())
words=[input().strip() for _ in range(n)]

alphabets={}
num=9

for word in words:
    len_word=len(word)
    for i in range(len_word):
        if word[i] not in alphabets:
            alphabets[word[i]]=10**(len_word-i)
        else: alphabets[word[i]]+=10**(len_word-i)
exp=''

hashResult = list(dict(sorted(alphabets.items(), reverse=True, key= lambda x: x[1])).keys())
num=9
for hr in hashResult:
    alphabets[hr]=str(num)
    num-=1

form=''
for word in words:
    temp=''
    for w in word:
        temp+=alphabets[w]
    form+=str(int(temp))+'+'
print(form)
print(eval(form[:-1]))

좋은 웹페이지 즐겨찾기