Post

๐Ÿฃ 36์ง„์ˆ˜

1. ๋ฌธ์ œ ๋งํฌ

1036๋ฒˆ: 36์ง„์ˆ˜


2. ์ฝ”๋“œ

Python3 34148KB 72ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# time : 50'

import sys
from collections import defaultdict

input = sys.stdin.readline
n = int(input())
str_list, num_list = [], []
num_cnt = defaultdict(int) # ์ˆซ์ž ์ค‘์š”๋„ ํšŸ์ˆ˜ ์ฒดํ‚น ๋”•์…”๋„ˆ๋ฆฌ

'''
๋ฌธ์ž์—ด์„ 36์ง„์ˆ˜ ์ˆซ์ž ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๋ฆฌํ„ด & ์ค‘์š”๋„ ์ฒดํ‚น
- ์ค‘์š”๋„ : ์ž๋ฆฟ์ˆ˜ + {'Z' - ํƒ€๊ฒŸ๊ฐ’}
'''
def str_to_notation(num_cnt, target):
    rst = []
    for i in range(len(target)):
        if (target[i].isdigit()):
            t = int(target[i])
        else:
            t = (ord(target[i]) - ord('A')) + 10
        num_cnt[t] += pow(36, len(target) - 1 - i) * (35 - t)
        rst.append(t)
    return rst


''' ์ˆซ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ์ตœ์ข… ๋ฌธ์ž์—ด๋กœ ๋ฆฌํ„ด '''
def num_list_to_str(num_list):
    rst = 0 # 36์ง„์ˆ˜ ์ˆซ์ž ํ˜•ํƒœ๋“ค์˜ ์ „์ฒด ํ•ฉ
    fin_str = ''
    for i in range(n):
        target = 0
        for j in range(len(num_list[i]) - 1, -1, -1):
            target = target + num_list[i][j] * \
                pow(36, len(num_list[i]) - 1 - j)
        rst += target

    while rst: # 36์ง„์ˆ˜์— ๋”ฐ๋ฅธ ๋ฌธ์ž ๋ณ€ํ™˜
        t = rst % 36
        if (t < 10):
            fin_str = str(t) + fin_str
        else:
            fin_str = chr(t - 10 + ord('A')) + fin_str
        rst //= 36
    return fin_str if fin_str else '0'  # 0์ผ ๋•Œ ๊ณต๋ฐฑ ๋ฆฌํ„ด exception [TC 87%]


for _ in range(n):
    str_num = input().rstrip()
    while (str_num[0] == '0' and len(str_num) > 1):  # trim 0 ์ฒ˜๋ฆฌ exception [TC 87%]
        str_num = str_num[1:]
    notation = str_to_notation(num_cnt, str_num)
    num_list.append(notation)

k = int(input())

# ์ค‘์š”๋„์— ๋”ฐ๋ฅธ ์ •๋ ฌ
list_cnt_notation = list(num_cnt.items())
list_cnt_notation.sort(key=lambda x: x[1], reverse=True)

for i in range(min(k, len(list_cnt_notation))):
    t = list_cnt_notation[i][0]
    for j in range(n):
        for l in range(len(num_list[j])):
            if num_list[j][l] == t:
                num_list[j][l] = 35  # Z๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ


print(num_list_to_str(num_list))



This post is licensed under CC BY 4.0 by the author.