diff --git a/java/src/boj1406/Main.java b/java/src/boj1406/Main.java new file mode 100644 index 0000000..dba8cea --- /dev/null +++ b/java/src/boj1406/Main.java @@ -0,0 +1,68 @@ +package boj1406; + +import java.io.*; +import java.util.LinkedList; +import java.util.Stack; +import java.util.StringTokenizer; + +public class Main { + + private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); + + public static void main(String[] args) throws IOException { + Stack leftStack = new Stack<>(); + Stack rightStack = new Stack<>(); + String init = readLine(); + for (char c : init.toCharArray()) { + leftStack.push(c); + } + + int n = Integer.parseInt(readLine()); + + for (int i = 0; i < n; i++) { + StringTokenizer tokenizer = new StringTokenizer(readLine()); + String command = tokenizer.nextToken(); + + switch (command) { + case "L": // 커서를 왼쪽으로 이동 + if (!leftStack.isEmpty()) { + rightStack.push(leftStack.pop()); + } + break; + case "D": // 커서를 오른쪽으로 이동 + if (!rightStack.isEmpty()) { + leftStack.push(rightStack.pop()); + } + break; + case "B": // 커서 왼쪽의 문자 삭제 + if (!leftStack.isEmpty()) { + leftStack.pop(); + } + break; + case "P": // 커서 왼쪽에 문자 추가 + char toAdd = tokenizer.nextToken().charAt(0); + leftStack.push(toAdd); + break; + } + } + + while (!leftStack.isEmpty()) { + rightStack.push(leftStack.pop()); + } + while (!rightStack.isEmpty()) { + writer.write(rightStack.pop()); + } + + writer.flush(); + writer.close(); + } + + private static String readLine() { + try { + return reader.readLine(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +}