import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
static Stack<Character> leftStack=new Stack<Character> ();
static Stack<Character> rightStack=new Stack<Character> ();
public static void main(String[] args) {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
try {
String startStr=br.readLine();
for(int j=0;j<startStr.length();j++){
leftStack.push(startStr.charAt(j));
}
String tempCnt=br.readLine();
int testCase=Integer.parseInt(tempCnt);
for(int i=0;i<testCase;i++){
String tempCommand=br.readLine();
StringTokenizer st=new StringTokenizer(tempCommand);
while(st.hasMoreElements()){
String commandStr=(String)st.nextElement();
if("P".equals(commandStr)){
String pStr=(String)st.nextElement();
leftStack.push(pStr.charAt(0));
}else if("L".equals(commandStr)){
if(leftStack.size()>0){
Character leftTempChar=leftStack.pop();
rightStack.push(leftTempChar);
}
}else if("D".equals(commandStr)){
if(rightStack.size()>0){
Character rightTempChar=rightStack.pop();
leftStack.push(rightTempChar );
}
}else if("B".equals(commandStr)){
if(leftStack.size()>0){
leftStack.pop();
}
}
}
}
StringBuffer lsf=new StringBuffer();
StringBuffer rsf=new StringBuffer();
for (Character lchar : leftStack) {
lsf.append(lchar.toString());
}
for (Character rchar : rightStack) {
rsf.append(rchar.toString());
}
bw.write(lsf.toString()+rsf.reverse().toString());
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
'알고리즘 > 스택' 카테고리의 다른 글
1894번 스택수열 (0) | 2020.06.21 |
---|---|
9012번 괄호 (0) | 2020.06.11 |
9093번 단어 뒤집기 (0) | 2020.06.07 |
10828번 스택 (0) | 2020.06.07 |