풀이
시작 커서는 항상 처음 입력받은 문자의 끝이기 때문에 스택으로 Left, Right를 나누고 입력받은 모든 값을 Left에 push, 이후 연산들을 진행하며 커서가 왼쪽으로 갈 시 해당 값을 pop시키고 Right에 push해준다.
주의사항
1. stack.size()로 for문을 돌게되면 pop()을 시키면서 사이즈가 줄어가므로 변수로 사이즈를 받은 후 시행 해야함.
소스코드
public class Baekjoon1406 {
static public void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
Stack<String> left = new Stack<>();
Stack<String> right = new Stack<>();
//string
String str = st.nextToken();
//num of operation
st = new StringTokenizer(br.readLine());
int num = Integer.parseInt(st.nextToken());
//length of string
int size = str.length();
//substring으로 나눌경우
// for(int i=0; i<size; i++)
// left.push(str.substring(i,i+1));
for(int i = 0; i<size; i++) {
left.push(String.valueOf(str.charAt(i)));
}
for(int i = 0; i<num; i++){
st = new StringTokenizer(br.readLine());
String op = st.nextToken();
if(op.equals("L")){
if(!left.isEmpty())
right.push(left.pop());
}
else if(op.equals("D")){
if(!right.isEmpty())
left.push(right.pop());
}
else if(op.equals("B")){
if(!left.isEmpty())
left.pop();
}
else{
left.push(st.nextToken());
}
}
while(!left.isEmpty()){
right.push(left.pop());
}
while(!right.isEmpty()){
System.out.print(right.pop());
}
}
}