boj)2504 - 괄호의 값

2020. 10. 14. 14:42PS/boj

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

public class boj_2504 {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        String str = sc.nextLine();

        Stack<String> stack = new Stack<>();
        int roundBracket = 0;
        int squareBracket = 0;

        for (int i = 0; i < str.length(); i++) {
            String s = str.substring(i, i + 1);

            switch (s) {
                case "(":
                    roundBracket++;
                    stack.push(s);
                    break;
                case "[":
                    squareBracket++;
                    stack.push(s);
                    break;
                case ")":
                    roundBracket--;
                    if (roundBracket > -1) {

                        if (stack.peek().equals("(")) {
                            stack.pop();
                            stack.push("2");
                        } else {
                            int roundResult = 0;

                            while (!stack.isEmpty()) { // 괄호안의 수 처리
                                if (stack.peek().equals("[")) { // 잘못된 문자열 처리
                                    bw.write("0");
                                    bw.flush();
                                    return;
                                } else if (stack.peek().equals("(")) {
                                    stack.pop();
                                    roundResult *= 2;
                                    stack.push(String.valueOf(roundResult));
                                    break;
                                } else { 
                                    roundResult += Integer.parseInt(stack.pop()); 
                                }
                            }
                        }
                    }
                    break;
                case "]":
                   squareBracket--;

                   if (squareBracket > -1) {
                       if (stack.peek().equals("[")) {
                           stack.pop();
                           stack.push("3");
                       } else {
                           int squareResult = 0;

                           while (!stack.isEmpty()) {
                                if (stack.peek().equals("(")) {
                                    bw.write("0");
                                    bw.flush();
                                    return;
                                } else if (stack.peek().equals("[")) {
                                    stack.pop();
                                    squareResult *= 3;
                                    stack.push(String.valueOf(squareResult));
                                    break;
                                } else {
                                    squareResult += Integer.parseInt(stack.pop());
                                }
                           }
                       }
                   }
                   break;
            }

            if (squareBracket < 0 || roundBracket < 0) {
                bw.write("0");
                bw.flush();
                return;
            }
        }

        if (squareBracket != 0 || roundBracket != 0) {
            bw.write("0");
            bw.flush();
            return;
        }

        int ans = 0 ;
        while (!stack.isEmpty()) {
            ans += Integer.parseInt(stack.pop());
        }
        bw.write(String.valueOf(ans));
        bw.flush();
    }
}

 

- 실패

 

- 여러가지 찾아봤는데 그나마 이해가 제일 잘되서 이걸로 따라갔음

- 여는 괄호는 상관이 없는데 닫은 괄호 처리하기가 까다로

   예외도 처리해야 하고 이게 곱하는 괄호인지 더하는 괄호인지도 처리해야함 

- 여기에서는 바로앞과 붙어있는 괄호일때 이걸 숫자로 바꿔서 다시 스택에 넣고

   아닐경우에는 여는괄호를 만날때까지 숫자들을 더하고 마지막엔 곱해줌

- 반복문을 끝까지 돌았으면 마지막엔 숫자만 남아있고 이 숫자들을 더한 값이 ans

'PS > boj' 카테고리의 다른 글

boj)10866 - 덱  (0) 2020.10.14
boj)4889 - 안정적인 문자열  (0) 2020.10.14
boj)4949 - 균형잡힌 세상  (0) 2020.10.13
boj)6198 - 옥상 정원 꾸미기  (0) 2020.10.13
boj)2164 - 카드2  (0) 2020.10.12