๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
4๏ธโƒฃ Python/Problem Solving

[Python Algorithm] AC BOJ #5430

by seolhee2750 2022. 5. 24.
๋ฌธ์ œ

https://www.acmicpc.net/problem/5430

 

5430๋ฒˆ: AC

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด์„œ, ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์ •์ˆ˜ ๋ฐฐ์—ด์— ํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ, ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ์—๋Š” error๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import sys

t = int(sys.stdin.readline().strip())
for _ in range(t):
    p = sys.stdin.readline().strip()
    n = int(sys.stdin.readline().strip())
    x = sys.stdin.readline().strip()
    lst = list(x[1:len(x)-1].split(","))

    rev = False
    front = 0
    back = 0
    error = False

    for i in p:
        if i == "R":
            if rev:
                rev = False
            else:
                rev = True
        else:
            if rev:
                back += 1
            else:
                front += 1
            if front + back > n:
                error = True
        if error:
            break

    if error:
        print("error")
    else:
        if rev:
            print("[" + ",".join(lst[::-1][back:(n-front)]) + "]")
        else:
            print("[" + ",".join(lst[front:(n-back)]) + "]")

๐Ÿ‘‰ ๋ฌธ์ž์—ด ๋ฌธ์ œ

R์ด๋‚˜ D ๋ช…๋ น์„ ์ง„์งœ๋กœ ํ•˜๋‚˜์”ฉ ์—ฐ์‚ฐํ•  ๊ฒฝ์šฐ ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ์˜ค๋ž˜๊ฑธ๋ฆด ๊ฒƒ์ด๋ฏ€๋กœ

R์˜ ์œ ๋ฌด๋กœ ํ˜„์žฌ ๋ฌธ์ž์—ด์ด ๋’ค์ง‘ํ˜€์žˆ๋Š”์ง€ ์•„๋‹Œ์ง€๋ฅผ ํŒ๋‹จํ•˜๊ณ , (rev ๋ณ€์ˆ˜)

D ๋ช…๋ น์ด ์žˆ์„ ์‹œ ์•ž์—์„œ ์‚ญ์ œํ•  ๊ฒƒ์ธ์ง€ ๋’ค์—์„œ ์‚ญ์ œํ•  ๊ฒƒ์ธ์ง€ ์ฒดํฌํ•ด์ฃผ์—ˆ๋‹ค. (front, back ๋ณ€์ˆ˜)

๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์—, ์•ž์—์„œ ๋ช‡ ๊ฐœ, ๋’ค์—์„œ ๋ช‡ ๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์‚ญ์ œํ•  ๊ฒƒ์ธ์ง€,

๋ฌธ์ž์—ด์„ ๋’ค์ง‘์–ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ๋ชจ๋‘ ๋ฐ˜์˜ํ•˜์—ฌ์„œ ๊ฒฐ๊ณผ๋กœ ์ถœ๋ ฅํ•ด์ฃผ์—ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ

๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ, ์ง„์งœ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด์•„๋‹ˆ๋ผ

"[", "]", "," ๋ฌธ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ ๋ชจ์–‘์œผ๋กœ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ๋งŒ ์ฃผ์˜ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.

๋Œ“๊ธ€