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

21.02.20 - [BOJ] 5397. 키로거 #17

Closed
suhyunsim opened this issue Feb 19, 2021 · 0 comments
Closed

21.02.20 - [BOJ] 5397. 키로거 #17

suhyunsim opened this issue Feb 19, 2021 · 0 comments
Assignees
Labels
성공 맞은 문제 실버 BOJ - 실버

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Feb 19, 2021

문제

핵심 아이디어

  • 1406.에디터 문제와 동일한 풀이
  • 스택 두 개 두고 가운데가 커서라고 생각

어려운 점, 실수

풀이

package main.java.com.poogle.BOJ.Q5397;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        while (n-- > 0) {
            Stack<Character> left = new Stack<>();
            Stack<Character> right = new Stack<>();
            String input = br.readLine();
            for (int i = 0; i < input.length(); i++) {
                if (input.charAt(i) == '<') {
                    if (!left.isEmpty()) right.push(left.pop());
                } else if (input.charAt(i) == '>') {
                    if (!right.isEmpty()) left.push(right.pop());
                } else if (input.charAt(i) == '-') {
                    if (!left.isEmpty()) left.pop();
                } else {
                    left.push(input.charAt(i));
                }
            }
            StringBuilder sb = new StringBuilder();
            while (!left.isEmpty()) {
                right.push(left.pop());
            }
            while (!right.isEmpty()) {
                sb.append(right.pop());
            }
            System.out.println(sb.toString());
        }
    }
}
@suhyunsim suhyunsim added the 실버 BOJ - 실버 label Feb 19, 2021
@suhyunsim suhyunsim added this to the 2월 3주 차 milestone Feb 19, 2021
@suhyunsim suhyunsim self-assigned this Feb 19, 2021
@suhyunsim suhyunsim added the 성공 맞은 문제 label Mar 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
성공 맞은 문제 실버 BOJ - 실버
Projects
None yet
Development

No branches or pull requests

1 participant