๋ฐฑ์ค€ 5430 AC

passยท2022๋…„ 10์›” 2์ผ
0

์•Œ๊ณ ๋ฆฌ์ฆ˜

๋ชฉ๋ก ๋ณด๊ธฐ
19/35

๋ฌธ์ œ

๐Ÿ‘€ ๋ฌธ์ œ ์‚ฌ์ดํŠธ : https://www.acmicpc.net/problem/5430






ํ’€์ด

๋‚œ์ด๋„ : Gold V

์ด ๋ฌธ์ œ๋Š” ๋ฐฐ์—ด์—์„œ popleft์™€ reverse๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ํ•˜์ง€๋งŒ, ๋ฌธ์ œ์—์„œ reverse๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ๋งˆ๋‹ค ๋ฐฐ์—ด์˜ reverse๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ reverse๊ฐ€ ์ž…๋ ฅ๋˜๋Š” ๊ฒƒ์€ ๋ณ€์ˆ˜๋กœ ์ €์žฅ๋งŒ ํ•ด๋‘๊ธฐ๋กœ ํ•˜๊ณ , pop์„ ํ•  ๋•Œ reverse์˜ ๊ฐ’์— ๋”ฐ๋ผ popleft() ๋˜๋Š” pop()์„ ์ง„ํ–‰ํ•˜์˜€๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ reverse๊ฐ€ ์ž…๋ ฅ๋œ ํšŸ์ˆ˜๊ฐ€ ํ™€์ˆ˜์ด๋ฉด ๋งˆ์ง€๋ง‰์— reverse๋ฅผ ํ•ด์ฃผ๊ณ  ๊ฒฐ๊ณผ ์ถœ๋ ฅ์„ ์ง„ํ–‰ํ•˜์˜€๋‹ค.



โœ” ์ฃผ์˜ํ•  ์ 

  • ์ด ๋ฌธ์ œ์— ๊ฒฝ์šฐ์—๋Š” ์ž…๋ ฅ๋ฐฉ์‹๊ณผ ์ถœ๋ ฅ๋ฐฉ์‹์ด ํŠน์ดํ•˜๋ฏ€๋กœ ์ฃผ์˜ํ•˜์—ฌ์•ผ ํ•œ๋‹ค.
  • python์—์„œ๋Š” ์ž…๋ ฅ์„ ํ•  ๋•Œ, rstrip์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ 'ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค'๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋˜๋ฏ€๋กœ ๋ฌธ์ž์—ด ์ž…๋ ฅ๊ณผ ํŒŒ์‹ฑ์—์„œ ์ฃผ์˜ํ•˜์—ฌ์•ผ ํ•œ๋‹ค.
  • python์—์„œ ์ด ๋ฌธ์ œ์™€ ๊ฐ™์ด ์ถœ๋ ฅ์ด ๋ณต์žกํ•  ๊ฒฝ์šฐ์— .join()์„ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.






์ฝ”๋“œ

import sys
from collections import deque

t = int(input())

for _ in range(t):
    commands = sys.stdin.readline().rstrip()
    n = int(input())
    arr = sys.stdin.readline().rstrip()[1:-1].split(",")
    q = deque(arr)

    isTrue = True
    reverse = 0

    if n == 0:
        q = []
    
    for command in commands:
        if command == "R":
            reverse += 1
        elif command == "D":
            if len(q) == 0:
                isTrue = False
                break

            if reverse % 2 == 0:
                q.popleft()
            else:
                q.pop()

    if isTrue:
        if reverse % 2 == 0:
            print("[" + ",".join(q) + "]")
        else:
            q.reverse()
            print("[" + ",".join(q) + "]")
    else:
        print("error")
profile
์•ˆ๋“œ๋กœ์ด๋“œ ๊ฐœ๋ฐœ์ž ์ง€๋ง์ƒ

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