from sys import stdin from collections import deque defsolution(command,l): iflen(l)<command.count('D'): return'error' for c in command: if c=="R": l.reverse() elif c=="D": l.popleft() return'['+','.join(l)+']'
T = int(input()) ans = [] for _ inrange(T): command = stdin.readline() n = int(stdin.readline().rstrip()) l = deque(stdin.readline().rstrip()[1:-1].split(',')) if n==0: l = [] print(solution(command,l))
따라서 R일때마다 뒤집지 않고, point 변수를 만들어주어 reverse일때마다 point변수를 바꾸어주고 pop은 reverse 마다 방향을 바꾸어 주어 pop을 해주는 방식으로 구현하였다.
예를 들어 reverse 할때마다 point값을 바꾸어주며 D일때 point값을 확인해 popleft할지 pop할지를 결정한다.
from sys import stdin from collections import deque defsolution(command,l): point = 'left' iflen(l)<command.count('D'): return'error' for c in command: if c=="R": if point =='left': point ='right' else: point = 'left' elif c=="D": if point =='left': l.popleft() else: l.pop() if point =='left': return'['+','.join(l)+']' else: l.reverse() return'['+','.join(l)+']'
T = int(input()) ans = [] for _ inrange(T): command = stdin.readline() n = int(stdin.readline().rstrip()) l = deque(stdin.readline().rstrip()[1:-1].split(',')) if n==0: l = [] print(solution(command,l))
해결이 되었다!!
무조건 문제의 조건을 구현하기 보다는 보다 효율적인 방법을 찾는것이 바람직 하다는것을 느꼈다.