import java.util.*;
import java.io.*;
class Main {
public static void main(String args[]) throws IOException {
Stack<Character> stack1 = new Stack<Character>();
Stack<Character> stack2 = new Stack<Character>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String s = br.readLine();
int len = s.length();
for(int i = 0 ; i < len ; i++) {
stack1.push(s.charAt(i));
}
int count = Integer.parseInt(br.readLine());
StringTokenizer st;
for(int i= 0 ; i < count ; i ++) {
st = new StringTokenizer(br.readLine());
char type = st.nextToken().charAt(0);
switch(type) {
case 'P':
stack1.push(st.nextToken().charAt(0));
break;
case 'L':
if(!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
break;
case 'D':
if(!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
break;
case 'B':
if(!stack1.isEmpty()) {
stack1.pop();
}
break;
}
}
while(!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
while(!stack1.isEmpty()) {
sb.append(stack1.pop());
}
System.out.println(sb.reverse());
}
}
스택 2개를 활용하여 커서를 관리해봤다.