Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[S2] 1406 에디터 #136

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions java/src/boj1406/Main.java
Original file line number Diff line number Diff line change
@@ -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<Character> leftStack = new Stack<>();
Stack<Character> 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);
}
}
}
Loading