๋ฌธ์
๋ฐฑ์ค ์จ๋ผ์ธ ์ ์ง - 18238๋ฒ
ํ์ด ๊ณผ์
ํ์ ํ์ ์ข ๋๋ ์ฐ๋ก ๋๋ ค์ ์ฃผ์ด์ง ๋ฌธ์์ด์ ๋ง๋๋ ์ต์ ์ด๋ ํ์๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ์ ๋๋ค.
๋ชจ๋ ๋ฌธ์๋ฅผ ์์คํค์ฝ๋๋ฅผ ํ์ฉํด ์ธ๋ฑ์ค ๊ฐ์ ๊ตฌํ๊ณ ์๊ณ๋ฐฉํฅ, ๋ฐ์๊ณ๋ฐฉํฅ ํ์ ์ฐ์ฐ ์ค ์ต์๊ฐ์ ๊ตฌํด์ ๋ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
์ด๋ ๊ฒ ํํ์ผ๋ก ์ด๋ฃจ์ด์ง ์ํฉ์์ ๋ ์ธ๋ฑ์ค ๊ฐ์ ๊ฑฐ๋ฆฌ ์ฐจ์ด๊ฐ์ ๊ตฌํ๋ ์์ด๋์ด๋ฅผ ๋ฐฐ์ธ ์ ์๋ ๋ฌธ์ ์์ต๋๋ค. ๐
์ฝ๋
import sys
alphabets = list(map(chr, range(ord('A'), ord('Z') + 1)))
target = sys.stdin.readline().strip()
def solution():
answer = 0
cursor = 0
for c in target:
target_idx = ord(c) - ord('A')
clockwise = cursor - target_idx + 26 if cursor - target_idx < 0 else cursor - target_idx
counter_clockwise = target_idx - cursor + 26 if target_idx - cursor < 0 else target_idx - cursor
answer += min(clockwise, counter_clockwise)
cursor = target_idx
return answer
print(solution())
๋ฐ์ํ
'๐ algorithm > boj' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ 11403 - ๊ฒฝ๋ก ์ฐพ๊ธฐ (0) | 2021.03.18 |
---|---|
BOJ 18243 - Small World Network (0) | 2021.03.18 |
BOJ 14754 - Pizza Boxes (0) | 2021.03.18 |
BOJ 15723 - n๋จ ๋ ผ๋ฒ (0) | 2021.03.18 |
BOJ 14496 - ๊ทธ๋, ๊ทธ๋จธ๊ฐ ๋์ด (0) | 2021.03.16 |
๐ฌ ๋๊ธ