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

[S2] 1406 에디터 #136

merged 1 commit into from
Jan 9, 2025

Conversation

wwan13
Copy link
Owner

@wwan13 wwan13 commented Jan 9, 2025

Problem Info

Code

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);
        }
    }
}

Note

@github-actions github-actions bot added data_structures 알고리즘: data_structures linked_list 알고리즘: linked_list stack 알고리즘: stack labels Jan 9, 2025
@github-actions github-actions bot changed the title solve : 1406 에디터 [S2] 1406 에디터 Jan 9, 2025
@wwan13 wwan13 merged commit ef480d5 into main Jan 9, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
data_structures 알고리즘: data_structures linked_list 알고리즘: linked_list stack 알고리즘: stack
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant