๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm

[Algorithm/Java][๋ฐฑ์ค€] 2504๋ฒˆ ๊ด„ํ˜ธ์˜ ๊ฐ’

๋ฐ˜์‘ํ˜•

[BOJ] 2504๋ฒˆ ๊ด„ํ˜ธ์˜ ๊ฐ’

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

๋ฌธ์ œ ์ ‘๊ทผ

Stack์„ ์ด์šฉํ•ด์„œ ์—ด๋ฆฐ ๊ด„ํ˜ธ๋ฅผ ์ €์žฅํ•˜๊ณ , ๊ด„ํ˜ธ๊ฐ€ ๋‹ซํž ๋•Œ X๊ฐ’์„ ๋‹ค์‹œ ์Šคํƒ์— ์ €์žฅํ•œ๋‹ค.
๊ด„ํ˜ธ ์†์— ์ˆซ์ž๊ฐ€ ์žˆ์œผ๋ฉด ๊ทธ ์ˆซ์ž๋“ค์„ ๋‹ค ๋”ํ•ด์„œ ํ•ด๋‹น ๊ด„ํ˜ธ ๊ฐ’์„ ๊ณฑํ•ด์ฃผ์—ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์Šคํƒ์„ Character ์ž๋ฃŒํ˜•์œผ๋กœ ํ•˜๋ฉด ๊ด„ํ˜ธ์™€ ์ˆซ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์—, String์œผ๋กœ ํ•ด์„œ ์ด ๊ฒฝ์šฐ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.
์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ๋ณต์žกํ•˜๊ณ  ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ๊ฐ€ ์•„๋‹Œ๊ฒƒ์„ ๊ฒ€์‚ฌํ•˜๊ธฐ๊ฐ€ ๊นŒ๋‹ค๋กœ์› ๋‹ค...

Code

import java.util.Scanner;
import java.util.Stack;

public class BOJ2504 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.next();
        Stack<String> st = new Stack<>();
        int sum = 0;
        for(int i = 0; i<input.length(); i++){
            if(input.charAt(i) == '('){
                st.push("(");
            } else if(input.charAt(i) == '['){
                st.push("[");
            } else if(input.charAt(i)== ')'){
                int temp = 0;
                if(st.isEmpty()){
                    sum = -1;
                    break;
                }
                while(!st.isEmpty() && !st.peek().equals("(")){
                    String num = st.pop();
                    if(!Character.isDigit(num.charAt(0))){
                        sum = -1;
                        break;
                    }
                    temp += Integer.parseInt(num);
                }
                if(sum == -1|| st.isEmpty()) {
                    sum = -1;
                    break;
                }
                if(temp == 0) temp = 2;
                else temp = temp * 2;
                st.pop();
                st.push(String.valueOf(temp));
            } else if(input.charAt(i) == ']'){
                int temp = 0;
                if(st.isEmpty()){
                    sum = -1;
                    break;
                }
                while(!st.isEmpty() && !st.peek().equals("[")){
                    String num = st.pop();
                    if(!Character.isDigit(num.charAt(0))){
                        sum = -1;
                        break;
                    }
                    temp += Integer.parseInt(num);
                }
                if(sum == -1|| st.isEmpty()){
                    sum = -1;
                    break;
                }
                if(temp == 0) temp = 3;
                else temp = temp * 3;
                st.pop();
                st.push(String.valueOf(temp));
            }
        }
        if(sum == -1){
            sum = 0;
        } else {
            while(!st.isEmpty()){
                String num = st.pop();
                if(!Character.isDigit(num.charAt(0))){
                    sum = 0;
                    break;
                }
                sum += Integer.parseInt(num);
            }
        }
        System.out.print(sum);
    }
}
๋ฐ˜์‘ํ˜•