fork download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Stack;
  5. import java.util.StringTokenizer;
  6.  
  7. public class Main{
  8.  
  9. public static void main(String argv[]) throws IOException
  10. {
  11.  
  12.  
  13. Stack<String> stack1 = new Stack<String>();
  14. Stack<String> stack2 = new Stack<String>();
  15. int num;
  16. String str = br.readLine();
  17.  
  18.  
  19. while(c.hasMoreTokens())
  20. stack1.push(c.nextToken());
  21.  
  22. num = Integer.parseInt(br.readLine());
  23.  
  24. for(int i=0; i<num; i++)
  25. {
  26. String res = br.readLine();
  27. StringTokenizer ne = new StringTokenizer(res, " ");
  28.  
  29. String cmd = ne.nextToken();
  30.  
  31. if(cmd.equals("P"))
  32. {
  33. stack1.push(ne.nextToken());
  34. }
  35. else if(cmd.equals("L"))
  36. {
  37. if(!stack1.empty())
  38. stack2.push(stack1.pop());
  39. }
  40. else if(cmd.equals("D"))
  41. {
  42. if(!stack2.empty())
  43. {
  44. stack1.push(stack2.pop());
  45. }
  46. }
  47. else if(cmd.equals("B"))
  48. {
  49.  
  50. if(!stack1.empty())
  51. {
  52. stack1.pop();
  53. }
  54. }
  55.  
  56. }
  57. while(!stack1.empty()) {//stack1이 비어있지 않을 때 까지 반복
  58. stack2.push(stack1.pop());
  59. }
  60. StringBuilder sb = new StringBuilder();
  61. while(!stack2.empty())
  62. {
  63. sb.append(stack2.pop());
  64. }
  65. System.out.println(sb);
  66. stack1.clear();
  67. stack2.clear();
  68.  
  69. }
  70.  
  71. }
  72.  
  73.  
Success #stdin #stdout 0.09s 27496KB
stdin
abc
3
L
B
B
stdout
abc