jae_coding

(백준 알고리즘 문제풀이) 1339번 단어 수학 본문

알고리즘 문제/백준

(백준 알고리즘 문제풀이) 1339번 단어 수학

재코딩 2022. 7. 5. 18:07
반응형

문제

문제 링크

 

문제 접근

  • dictionary를 활용하면 문제에 대한 접근성이 좋을 것 같다.
  • dictionary key에 대한 value는 N개의 letter에서 몇번째 자리의 수가 key인지 알면 좋을 것 같다.
  • 예를 들어 2, GCF, ACDEB에서 C의 dictionary key는 1010으로 GCF에서 C의 자리 10과 ACDEB의 C의 자리 1000을 더해준 값과 같이 dictionary를 구성해준다.
  • 여기서 몇번째 자리가 가장 높은지에 대한 index리스트를 구성해준다. 이때 index리스트는 내림차순으로 구성해준다.
  • 그리고 결과는 자리수가 높은 순서대로 9부터 1씩 감소하면서 문자의 index를 정해준 후 index리스트를 곱하여 준 후 합을 구하여 준다.

 

코드

import sys


def main():
    N = int(sys.stdin.readline())
    letter_list = list()
    for _ in range(N):
        letter_list.append(sys.stdin.readline())

    index_dict = dict()
    for i in letter_list:
        count = len(i) - 1
        for j in i:
            if j in index_dict:
                index_dict[j] += pow(10, count)
            else:
                index_dict[j] = pow(10, count)
            count -= 1
    # print(index_dict)

    index_list = list()
    for value in index_dict.values():
        index_list.append(value)
    index_list.sort(reverse=True)
    # print(index_list)

    result = 0
    index = 9
    for k in range(len(index_list)):
        result += index_list[k] * index
        index -= 1
    print(result)


main()

느낀점

파이썬의 자료구조인 dictionary의 key와 value를 활용하여 문자의 자리를 높은 순서대로 index를 해주고, 그 index의 자리를 구하여 줌으로써, 데이터 값의 정렬과 컴퓨터 상에서의 정리를 이해할 수 있는 참으로 흥미로운 문제였다.

반응형
Comments