๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’ป Computer Science/- Data Structure, Algorithm

[์•Œ๊ณ ๋ฆฌ์ฆ˜ PS] ๋ฐฑ์ค€ 1406 ์—๋””ํ„ฐ ์ž๋ฐ” ๋ฌธ์ œ ํ’€์ด (์Šคํƒ ์ง์ ‘ ๊ตฌํ˜„ํ•ด์„œ ํ’€์–ด๋ณด๊ธฐ)

by Wonit 2021. 1. 20.

๋ฌธ์ œ

ํ•ด๋‹น ํฌ์ŠคํŒ…์€ ๋ฐฑ์ค€์˜ 1406๋ฒˆ ์—๋””ํ„ฐ ์˜ ์ ‘๊ทผ๊ณผ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค.
์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ

์˜ˆ์ œ ์ž…๋ ฅ & ์ถœ๋ ฅ

ํ•ด๊ฒฐ๋ฒ•

์ด ๋ฌธ์ œ๋Š” ์‚ฌ์‹ค ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค.


Cursor์ด๋ผ๋Š” ๊ฐœ๋…์€ LinkedList์—์„œ ์ ํ•ฉํ•ด ๋ณด์ด์ง€๋งŒ LinkedList๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ด์„œ ํ’€๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.


๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ์ ‘๊ทผ์„ ํ†ตํ•ด ํ’€์–ด์•ผ ํ•˜๋Š”๋ฐ, ๊ทธ ๋‹ค๋ฅธ ์ ‘๊ทผ์ด ๋ฐ”๋กœ ์Šคํƒ์ด๋‹ค.

์ ‘๊ทผ๋ฒ•

ํŽธ์ง‘๊ธฐ๊ฐ€ ์ง€์›ํ•˜๋Š” ๋ช…๋ น์–ด๋Š” ์ด 4๊ฐ€์ง€ ์ด๋‹ค.


์šฐ๋ฆฌ๋Š” ์ปค์„œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ Stack 2๊ฐœ๋กœ ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ’€์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

 

์ฒซ ๋ฒˆ์งธ ์Šคํƒ์—๋Š” ์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด๋“ค์ด ๋ชจ๋‘ ๋“ค์–ด๊ฐ„ ์ฑ„๋กœ ์‹œ์ž‘ํ•œ๋‹ค.


๊ทธ๋ฆฌ๊ณ  ๊ฐ๊ฐ์˜ ์ด๋™ ๋ช…๋ น์–ด๋ฅผ ๋งŒ๋‚  ๋•Œ ๋งˆ๋‹ค ์ปค์„œ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ์ˆ˜๋ฅผ ์„œ๋กœ ๋‹ค๋ฅธ ์Šคํƒ์— ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

 

  1. L : ์ปค์„œ๋ฅผ ์™ผ์ชฝ์œผ๋กœ ํ•œ ์นธ ์˜ฎ๊ธฐ๋Š” ๊ณผ์ •์€ ์™ผ์ชฝ์— ์žˆ๋Š” ์Šคํƒ์—์„œ ์ˆ˜ ํ•˜๋‚˜๋ฅผ ๋นผ์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์˜ฎ๊ธฐ๋ฉด ๋œ๋‹ค.
  2. D : ์ด๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์˜ค๋ฅธ์ชฝ ์Šคํƒ์—์„œ ์™ผ์ชฝ ์Šคํƒ์œผ๋กœ ์˜ฎ๊ธฐ๋ฉด ๋œ๋‹ค.
  3. B : ์ปค์„œ ์™ผ์ชฝ์˜ ๋ฌธ์ž์ด๋ฏ€๋กœ ์™ผ์ชฝ ์Šคํƒ์— top์„ popํ•œ๋‹ค.
  4. P $ : ์ปค์„œ ์™ผ์ชฝ์— ์ถ”๊ฐ€ํ•˜๋ฏ€๋กœ ์™ผ์ชฝ ์Šคํƒ์— push ํ•˜๋ฉด ๋œ๋‹ค.

์˜ค๋‹ต ํ›„๋ณด

์˜ค๋‹ต ํ›„๋ณด์—๋Š” ์Šคํƒ์ด ๋น„์–ด์žˆ์„ ๋•Œ pop์„ ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ L๊ณผ D ์—ฐ์‚ฐ์„ ํ•  ๋•Œ isEmpty()๋กœ ๊ฒ€์‚ฌ๋ฅผ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

์ •๋‹ต ์ฝ”๋“œ

์ด๋ฒˆ์—๋Š” ์Šคํƒ์„ ์ง์ ‘ ๊ตฌํ˜„ํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋ ค ํ•œ๋‹ค.


์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•œ ์Šคํƒ์ด๋‹ค.

import java.io.*;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        String words = br.readLine();
        Stack stack = new Stack();
        char[] word = words.toCharArray();
        for(char ch : word) {
            stack.push(ch);
        }
        Stack tempStack = new Stack();
        int n = Integer.parseInt(br.readLine());
        while(n-- > 0) {

            String cmd = br.readLine();

            if(cmd.charAt(0) == 'P') {
                cmdP(stack, cmd.charAt(2));
            }else {
                if(cmd.charAt(0) == 'L') {
                    cmdL(stack, tempStack);
                }else if(cmd.charAt(0) == 'D') {
                    cmdD(stack, tempStack);
                }else if(cmd.charAt(0) == 'B') {
                    cmdB(stack);
                }
            }
        }

        while(!stack.isEmpty()) {
            tempStack.push(stack.pop());
        }
        while(!tempStack.isEmpty()) {
            bw.write(tempStack.pop());
        }

        bw.flush();
        bw.close();
    }

    private static void cmdL(Stack stack, Stack tempStack) {
        if(!stack.isEmpty()) {
            tempStack.push(stack.pop());
        }
    }
    private static void cmdD(Stack stack, Stack tempStack) {
        if(!tempStack.isEmpty()) {
            stack.push(tempStack.pop());
        }
    }
    private static void cmdB(Stack stack) {
        if(!stack.isEmpty()) {
            stack.pop();
        }
    }
    private static void cmdP(Stack stack, char ch) {
        stack.push(ch);
    }
}
class Node {
    private char data;
    private Node link;

    Node(Node link, char data) {
        this.data = data;
        this.link = link;
    }

    char getData() {
        return data;
    }

    Node getLink() {
        return link;
    }
}
class Stack {
    Node top;

    Stack () {
        top = null;
    }
    boolean isEmpty() {
        return top == null;
    }

    void push(char data) {
        top = new Node(top, data);
    }

    char pop() {
        char ret = top.getData();
        top = top.getLink();
        return ret;
    }
}

์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋Œ“๊ธ€