[백준 -5430] AC

FeelingXD·2023년 8월 22일
0

문제풀이

목록 보기
19/34
post-thumbnail

❓ Problem

🤔 How

  1. 커맨드는 두가지의 경우로 이루어진 문자열이다. "R"은 뒤집기 그리고 "D" 는 버리기다.
  2. "R" 키워드를 마주칠때마다 진짜배열을 뒤집는 일이 발생한다면 뒤집을때마다 O(N) 의 시간이 소요되므로 시간 초과 날수있다. 그래서 "R" 키워드를 마주한다면 reverse 여부를 체크하는 flag를 추가하고 reverse 체크여부에 따라 "D"(버리기)시 pop 혹은 popleft를 수행하는것으로 O(1) 로 제거하였다.
  3. 최종 출력문에서 reverse 여부를 체크하여 한번만 정리하여 출력하였다.

⚠️ 주의

작성자는 이전에 마지막에 단순 배열을 출력하도록 하였을때 "틀렸습니다."를 받았다. 이유는 배열을 그대로 출력시 문제에서 요구하는 배열값과는 조금 다르게 추가적으로 띄어쓰기가 출력된다.

ex_list= [1,2]
# 문제가 원하는 출력
# expect [1,2]
print(ex_list)
# output .. [1, 2] 추가적인 띄어쓰기가 되어버린다..

그래서 출력문을 배열을 직접 출력하는것이아닌 문자열로 변경하여 출력하는것으로 수정해주었다.

print(f'[{",".join(q)}]' if not reverse_check else f'[{",".join(reversed(q))}]')

❗ Solve

# AC
import sys
from collections import deque
input = sys.stdin.readline

case = int(input())

# R 뒤집기 D 버리기

for _ in range(case):
    reverse_check = False
    error_check = False
    cmd = input().rstrip()
    l = int(input())
    li = list(input().rstrip()[1:-1].split(","))
    q = deque(list(filter(None, li)))
    for c in cmd:
        match c:
            case "R":
                reverse_check = False if reverse_check else True
                pass
            case "D":
                if q:
                    if reverse_check:
                        q.pop()
                    else:
                        q.popleft()
                else:
                    error_check = True
                    print("error")
                    break
                pass
    if not error_check:
        print(f'[{",".join(q)}]' if not reverse_check else f'[{",".join(reversed(q))}]')
profile
tistory로 이사갑니다. :) https://feelingxd.tistory.com/

0개의 댓글