문제링크 : https://school.programmers.co.kr/learn/courses/30/lessons/81303
def solution(n, k, cmd):
link = { i : [i-1, i+1] for i in range(n) }
link[0] = [None, 1]
link[n-1] = [n-2, None]
cur = k
ans = ['O'] * n
stack = []
for c in cmd:
if c == 'C':
ans[cur] = 'X'
prev,next = link[cur]
stack.append([prev,cur,next])
if next == None:
cur = link[cur][0]
else:
cur = link[cur][1]
if prev == None:
link[next][0] = None
elif next == None:
link[prev][1] = None
else:
link[prev][1] = next
link[next][0] = prev
elif c == 'Z':
prev, now , next = stack.pop()
ans[now] = 'O'
if prev == None:
link[next][0] = now
elif next == None:
link[prev][1] = now
else:
link[next][0] = now
link[prev][1] = now
else:
c1, c2 = c.split()
if c1 == 'U':
for _ in range(int(c2)):
cur = link[cur][0]
else:
for _ in range(int(c2)):
cur = link[cur][1]
return "".join(ans)