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.03.12 - [BOJ] 5430. AC #30

Closed
suhyunsim opened this issue Mar 11, 2021 · 0 comments
Closed

21.03.12 - [BOJ] 5430. AC #30

suhyunsim opened this issue Mar 11, 2021 · 0 comments
Assignees
Labels
실버 BOJ - 실버 실패 시도했지만 맞지 못한 문제

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Mar 11, 2021

문제

핵심 아이디어

  • 덱으로 풀기
  • flag 사용

어려운 점, 실수

  • reverse 명령일 때 그냥 플래그만 변경해가면서 removeFirst / removeLast 활용하면 되는데 자꾸 덱을 두개 두고 실제로 넣었다 빼도록 구현하려다 보니 풀지 못했다.
  • 덱의 장점을 활용하기
  • 빈 배열 들어왔을 때 예외처리 하기

풀이

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Deque;
import java.util.LinkedList;

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

        for (int tc = 1; tc <= t; tc++) {
            String command = br.readLine(); //rdd
            int n = Integer.parseInt(br.readLine()); //4
            String numC = br.readLine(); //[1,2,3,4]
            Deque<Integer> deque = new LinkedList<>();
            for (String s : numC.substring(1, numC.length() - 1).split(",")) {
                if (!s.equals(""))
                    deque.addLast(Integer.valueOf(s));
            }
            System.out.println(ac(deque, command));
        }
    }

    static String ac(Deque<Integer> deque, String commands) {
        boolean reverse = false;

        for (char command : commands.toCharArray()) {
            if (command == 'R')
                reverse = !reverse;
            else {
                if (deque.size() == 0)
                    return "error";
                if (reverse)
                    deque.removeLast();
                else
                    deque.removeFirst();
            }
        }

        StringBuilder sb = new StringBuilder("[");

        while (!deque.isEmpty()) {
            sb.append(reverse ? deque.removeLast() : deque.removeFirst());
            if (deque.size() != 0)
                sb.append(',');
        }
        sb.append(']');
        return sb.toString();
    }
}
@suhyunsim suhyunsim added 실버 BOJ - 실버 labels Mar 11, 2021
@suhyunsim suhyunsim added this to the 3월 2주 차 milestone Mar 11, 2021
@suhyunsim suhyunsim self-assigned this Mar 11, 2021
@suhyunsim suhyunsim changed the title 21.03.11 - [BOJ] 5430. AC 21.03.12 - [BOJ] 5430. AC Mar 11, 2021
@suhyunsim suhyunsim added the 실패 시도했지만 맞지 못한 문제 label Mar 20, 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