https://www.acmicpc.net/problem/1406
처음에는 하나하나 노가다로 풀었는데, 그러면 무조건 시간초과가 나오게 된다.
해결방법은 스택이 2개있고 그 사이에 커서가 있다고 생각하는 것이다.
만일 L이라면, 스택 1의 출력을 스택 2에 입력하면 된다 (스택 1이 비었으면 무시)
만일 D라면, 스택 2의 출력을 스택 1에 입력하면 된다(스택2가 비었으면 무시)
만일 B라면, 스택 1의 맨 위를 날리면 된다. (역시 비면 무시)
만일 p라면, 스택 1에 새 문자를 추가하면 된다.
풀이를 알고 나면 정말 간단한데 풀이가 떠올르지 않는다.
stack1 =list(input().strip())stack2 =[]M=int(input())for _ in range(M):
order=input().split() if order[0]=='L': if stack1:
stack2.append(stack1.pop()) elif order[0]=='D': if stack2:
stack1.append(stack2.pop()) elif order[0]=='B': if stack1:
stack1.pop() elif order[0]=='P': stack1.append(order[1])
print(''.join(stack1 + list(reversed(stack2))))