๐Ÿ˜Š ๋ฐฑ์ค€ 1339 : ๋‹จ์–ด ์ˆ˜ํ•™

3Juhwanยท2021๋…„ 2์›” 27์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
18/23

1339: ๋‹จ์–ด ์ˆ˜ํ•™

Greedy ๋ฌธ์ œ์ด๋‹ค.
์ด๋ฒˆ ๊ป€ ์กฐ๊ธˆ ๋‚œ์ด๋„๊ฐ€ ์žˆ์—ˆ๋‹ค. ํ’€๊ณ  ๋ณด๋‹ˆ, Gold 4 ๋ฌธ์ œ์˜€๋‹ค.


๐Ÿ“Œ Try 1

N = int(input())
words = dict()

for _ in range(N):
  word = list(input())
  digit = 1

  for w in reversed(word):
    words[w] = digit if w not in words else words[w] + digit
    digit *= 10

num, _sum = 9, 0
for w in sorted(words.items(), reverse=True, key=lambda x:x[1]):
  _sum += num * w[1]
  num -= 1

print(_sum)

์–ด๋–ป๊ฒŒ ํ’€๊นŒ ๊ณ ๋ฏผ์„ ํ–ˆ๊ณ , ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ๋– ์˜ฌ๋ž์ง€๋งŒ ์„ ๋œป ํ•˜๋‚˜๋ฅผ ์ •ํ•˜์ง€ ๋ชปํ–ˆ๋‹ค.
ํ•œ ๋ธ”๋กœ๊ทธ ๊ธ€์„ ์ฐธ๊ณ ํ•ด์„œ ๋ฐฉํ–ฅ์„ ์ •ํ–ˆ๊ณ  ์ฝ”๋“œ๋Š” ๋ชจ๋‘ ๋‚ด๊ฐ€ ์ž‘์„ฑํ–ˆ๋‹ค.

๋‚˜๋Š” dict๋ฅผ ์ด์šฉํ•ด์„œ ํ’€๊ณ , dict๋ฅผ ์ •๋ ฌํ–ˆ๋‹ค.
dict๋Š” unordered ์ž๋ฃŒ๊ตฌ์กฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ’€๊ธด ํ–ˆ์ง€๋งŒ,,, ๊ฐœ์šด์น˜ ์•Š์•˜๋‹ค.


๐ŸŽฏ ๋‹ค๋ฅธ ๋ถ„ ์ฝ”๋“œ

n = int(input()) 
word = [list(map(lambda x: ord(x)-65, input().rstrip())) for _ in range(n)] 
alpha = [0] * 26

for i in range(n): 
  j = 0   
  for w in word[i][::-1]: 
    alpha[w] += (10 ** j) 
    j += 1 

alpha.sort(reverse=True) 
ans, t = 0, 9 
for i in range(26): 
  if alpha[i] == 0:
    break
  ans += (t * alpha[i]) 
  t -= 1

print(ans)

double-list๋กœ ํ‘ธ์…จ๋‹ค.


๐ŸŽ Reference

profile
Codeforces์™€ USACO ํ’€์ด๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ๊ธ€๋„ ๊ณ„์† ์—…๋ฐ์ดํŠธ ๋ฉ๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€