본문 바로가기

알고리즘/백준 (Pyhthon)

[알고리즘] 백준 5397 키로거

test_case = int(input())
for _ in range(test_case):
    stack1 = []
    stack2 = []

    str = input()

    for i in range(len(str)):
        if str[i] == '>':
            if stack2:
                stack1.append(stack2.pop())
        elif str[i] == '<':
            if stack1:
                stack2.append(stack1.pop())
        elif str[i] == '-':
            if stack1:
                stack1.pop()
        else:
            stack1.append(str[i])
    stack1.extend(reversed(stack2))
    print(''.join(stack1))
''.join(stack1) #리스트를 스트링으로 합침
test_case = int(input())

for _ in range(test_case):
  left_stack = []
  right_stack = []
  data = input()
  
  for i in data:
    if i == '-':
    	if left_stack:
    		left_stack.pop()

    elif i == '<':
      if left_stack:
          right_stack.append(left_stack.pop())

    elif i == '>':
      if right_stack:
     	 left_stack.append(right_stack.pop())

    else:
      left_stack.append(i)

  left_stack.extend(reversed(right_stack))
  print(''.join(left_stack))