ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

Algorithm/Baekjoon

[Java] ๋ฐฑ์ค€ 5430๋ฒˆ - AC

๊ฐœ๋ฐœ๊ฐœ๊ตด๐Ÿธ 2022. 7. 14. 17:23

[๋ฌธ์ œ]

https://www.acmicpc.net/problem/5430

 

5430๋ฒˆ: AC

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด์„œ, ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์ •์ˆ˜ ๋ฐฐ์—ด์— ํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ, ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ์—๋Š” error๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net

[ํ’€์ด]

์•„๋ž˜์˜ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ํ’€์ดํ•˜์˜€์Šต๋‹ˆ๋‹ค.

https://girawhale.tistory.com/9

 

[๋ฐฑ์ค€] 5430๋ฒˆ: AC - JAVA

๋ฌธ์ œ ๋งํฌ BOJ 5430๋ฒˆ: AC 5430๋ฒˆ: AC ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด์„œ, ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์ •์ˆ˜ ๋ฐฐ์—ด์— ํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ, ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ์—๋Š” error๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. www.acmicpc.net ํ’€์ด ๊ณผ

girawhale.tistory.com

๋ฐฐ์—ด์„ ์ง์ ‘ ๋’ค์ง‘๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, start์™€ end์ ์„ ์ง€์ •ํ•ด๋†“๊ณ  ๋‘๊ฐœ๋ฅผ swapํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์ด์ค‘์ด์—ˆ์œผ๋‚˜, ์ž๊พธ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋‚˜์„œ ํ™•์ธํ•ด๋ณด๋‹ˆ ์ดˆ๋ฐ˜์— ๋ฐฐ์—ด์„ ์ดˆ๊ธฐํ™” ํ•˜๋Š” ๊ณผ์ •์—์„œ ์ˆซ์ž๋ฅผ ์ž˜๋ชป ๋ฝ‘์•„๋‚ด๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.ใ… ใ…  ๋˜ํ•œ ํ’€์ด๋ฅผ ์‚ดํŽด๋ณด๋‹ˆ Deque๋ฅผ ํ™œ์šฉํ•œ๋‹ค๋ฉด, ์‰ฝ๊ฒŒ ์•ž ๋’ค๋ฅผ ์ž˜๋ผ๋‚ผ์ˆ˜ ์žˆ๋‹ค๋Š”๊ฑธ ์•Œ๊ฒŒ๋˜์—ˆ๊ณ  Deque๋ฅผ ํ™œ์šฉํ•œ ํ’€์ด๋ฒ•์œผ๋กœ ์ง„ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

์šฐ์„  Stringํ˜•์‹์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ๋ฐฐ์—ด๊ฐ’์˜ ๋งจ์•ž([),๋งจ๋’ค(])๋ฅผ ์ž˜๋ผ๋‚ธ ํ›„, ","์„ ๊ธฐ์ค€์œผ๋กœ split()ํ•˜์—ฌ ์ˆซ์ž๋“ค์„ ๋ฝ‘์•„๋‚ธ ํ›„ Deque์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.

String arr = br.readLine();
Deque<Integer> deque = new LinkedList<>();
for (String s : arr.substring(1, arr.length() - 1).split(",")) {
	if (!s.equals("")) {
        deque.add(Integer.valueOf(s));
    }
}

๋‹ค์Œ์œผ๋กœ ACํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ˆ˜ํ–‰ํ•ด์•ผํ•  ๋™์ž‘๋“ค์ธ f๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉฐ R๊ณผ D๋ฅผ ์ˆ˜ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ R์ด ๋“ค์–ด์˜จ๋‹ค๋ฉด, ๊ฐ’์„ ์‚ญ์ œํ•˜๋Š” ๊ธฐ์ค€๊ฐ’ First์™€ Last๋ฅผ ๋ฐ”๊ฟ”์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. D๊ฐ€ ๋“ค์–ด์™”์„๋• Deque๊ฐ€ ๋น„์–ด์žˆ๋‹ค๋ฉด error๋ฅผ ์ถœ๋ ฅํ•˜์˜€๊ณ , ๋น„์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด ๊ธฐ์ค€๊ฐ’์ด ๋ฐ”๋ผ๋ณด๋Š” First๋‚˜ Last์—์„œ ๊ฐ’์„ ์ œ๊ฑฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

[์ฝ”๋“œ]

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

public class Main {
    // R : ๋’ค์ง‘๊ธฐ
    // D : ์ฒซ๋ฒˆ์งธ ์ˆ˜ ๋ฒ„๋ฆฌ๊ธฐ
    public static StringBuffer sb = new StringBuffer();
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        for(int i=0;i<T;i++) {
            String f = br.readLine();
            int n = Integer.parseInt(br.readLine());
            String arr = br.readLine();
            Deque<Integer> deque = new LinkedList<>();
            for (String s : arr.substring(1, arr.length() - 1).split(",")) {
                if (!s.equals("")) {
                    deque.add(Integer.valueOf(s));
                }
            }
            AC(deque, f);
        }
    }
    public static void AC(Deque<Integer> deque, String f) {
        boolean reverse = false;
        for(int i=0;i<f.length();i++) {
            if(f.charAt(i) == 'R') {
                reverse = !reverse;
            } else {
                if(deque.size() == 0) {
                    System.out.println("error");
                    return;
                }
                if(reverse) {
                    deque.removeLast();
                } else {
                    deque.removeFirst();
                }
            }
        }
        StringBuilder s = new StringBuilder("[");
        while(!deque.isEmpty()) {
            s.append(reverse ? deque.removeLast() : deque.removeFirst());
            if(deque.size() != 0) {
                s.append(",");
            }
        }
        s.append(']');
        System.out.println(s);
    }
}