🐣 알고리즘 삐약/💻 백준 삐약

50 삐약 : 백준 2504| 괄호의 값 [바킹독 문제 풀이|Stack|JAVA]

우주수첩 2023. 12. 29. 09:01
728x90

 

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

 

2504번: 괄호의 값

4개의 기호 ‘(’, ‘)’, ‘[’, ‘]’를 이용해서 만들어지는 괄호열 중에서 올바른 괄호열이란 다음과 같이 정의된다. 한 쌍의 괄호로만 이루어진 ‘()’와 ‘[]’는 올바른 괄호열이다. 만일 X

www.acmicpc.net

 

 

1. Buffered Reader / toCharArray 

package BKD_0x8_Stack_Application;

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

public class BOJ_2504 {
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String input = br.readLine();
        Stack<Character> stack = new Stack<>();
        int temp=1;
        int total=0;
        for(int i=0;i<input.length();i++){
            if (input.charAt(i)=='(') {
                stack.push('(');
                temp *= 2;
            }
            else if (input.charAt(i)=='[') {
                stack.push('[');
                temp *= 3;
            }
            else if (input.charAt(i)==')') {
                if (stack.isEmpty() || stack.peek() != '(') {
                    total = 0;
                    break;
                } else if (!stack.isEmpty() && input.charAt(i-1)== '(') {
                    total += temp;
                }
                stack.pop();
                temp /= 2;

            }
            else if (input.charAt(i)==']'){
                if(stack.isEmpty() || stack.peek() !='[') {
                    total=0;
                    break;
                }
                else if(!stack.isEmpty()&& input.charAt(i-1)=='[') {
                    total+=temp;
                }
                stack.pop();
                temp/=3;

            }

        }

        if(!stack.isEmpty()) System.out.println(0);
        else System.out.println(total);
    }
}

 

 

 

2. Buffered Reader / toCharAt 

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

public class Main {
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String input = br.readLine();
        Stack<Character> stack = new Stack<>();
        int temp=1;
        int total=0;
        for(int i=0;i<input.length();i++){
            if (input.charAt(i)=='(') {
                stack.push('(');
                temp *= 2;
            }
            else if (input.charAt(i)=='[') {
                stack.push('[');
                temp *= 3;
            }
            else if (input.charAt(i)==')') {
                if (stack.isEmpty() || stack.peek() != '(') {
                    total = 0;
                    break;
                } else if (!stack.isEmpty() && input.charAt(i-1)== '(') {
                    total += temp;
                }
                stack.pop();
                temp /= 2;

            }
            else if (input.charAt(i)==']'){
                if(stack.isEmpty() || stack.peek() !='[') {
                    total=0;
                    break;
                }
                else if(!stack.isEmpty()&& input.charAt(i-1)=='[') {
                    total+=temp;
                }
                stack.pop();
                temp/=3;

            }

        }

        if(!stack.isEmpty()) System.out.println(0);
        else System.out.println(total);
    }
}

 

 

 

 

 

 

아래 결과가 1번, 위의 결과가 2번 이다. 

 

 

 

 

👀 주목주목!

 

-  이전 문자 탐지 필요. == 문장 전체에 대한 정보 활용 요구

-  괄호의 올바른 대칭 요구

-  분배법칙 활용 시 효율적

 

고민한 점

1. 언제 temp 값에 각 괄호에 해당하는 값을 곱해야 하는가

- 처음 문제 해결 진행 시 괄호가 닫힐 때 temp 값에 곱하는 방향을 시도하였으나 실패

 

 

 

 

728x90