알고리즘

<백준> 5430번 파이썬 알고리즘

changha. 2021. 11. 27. 22:41
import sys
from collections import deque

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

    if n == 0:
      if 'D' in f:
        print("error")
        continue
      else:
        print("[]")
        continue
    e = 0
    # R 홀수 or 짝수 일때 경우 
    r = 0
    for i in f:
        if i == "R":
            r += 1
        else:
            if arr:
                if r % 2 == 0: #짝수일때
                    arr.popleft()
                else:
                    arr.pop()
            else:
              e = 1
              print("error")
              break
    if e == 0:
        if r % 2== 0:
            print("[" + ",".join(arr) + "]")
        else:
            arr.reverse()
            print("[" + ",".join(arr) + "]")