less than 1 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅˆ Silver 4


๐Ÿ“Œ ๋ฌธ์ œ

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


image

image


๐Ÿ“Œ ํ’€์ด

Stack, ์Šคํƒ์ด๋ž€ ๋Œ€ํ‘œ์ ์ธ LILO ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค.

์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ’์„ Stack์— ๋„ฃ๊ณ , ์ƒˆ ๊ฐ’์„ ๋„ฃ์„๋•Œ๋งˆ๋‹ค Top๊ณผ ๋น„๊ต๋ฅผ ํ•˜์—ฌ ๋‹ค๋ฅด๋ฉด ๋นผ๊ณ , ๊ฐ™์œผ๋ฉด ๋„ฃ์—ˆ๋‹ค.
๋˜ )๊ฐ€ ๋“ค์–ด์™”๋Š”๋ฐ ๋น„์–ด์žˆ์œผ๋ฉด NO๊ฐ€ ๋‚˜๋„๋ก ํ•˜์˜€๊ณ , ๋ชจ๋“  ์ž…๋ ฅ์„ ๋งˆ์ณค๋Š”๋ฐ Stack์— ๊ฐ’์ด ๋‚จ์•„์žˆ์œผ๋ฉด NO ๋ฉ”์„ธ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ ํ•˜์˜€๋‹ค.

์ด ๋ชจ๋“  ๊ณผ์ •์„ ๋งˆ์ณค์œผ๋ฉด, YES ๋ฉ”์„ธ์ง€๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.


๐Ÿ“Œ Code

package DS;

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

public class N9012 {
    static Stack<String> ps;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int num = Integer.parseInt(br.readLine());
        ps = new Stack<String>();

        for (int i = 0; i < num; i++) {
            ShowPS(br);
            ps.clear();
        }
    }
    public static void ShowPS(BufferedReader br) throws IOException{
        for (String str : br.readLine().split("")) {
            if (str.equals("(")) {
                ps.push(str);
            } else {
                if (ps.isEmpty()) {
                    System.out.println("NO");
                    return;
                } else {
                    ps.pop();
                }
            }
        }
        if (ps.isEmpty()) {
            System.out.println("YES");
        } else{
            System.out.println("NO");
        }
    }
}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜