Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

JAVA Developer Training

34. 2504번 괄호의 값 본문

백준 알고리즘

34. 2504번 괄호의 값

Romenest 2021. 10. 26. 14:47

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

 

2504번: 괄호의 값

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

www.acmicpc.net

 

 

(), [] 두개의 괄호 유형을 입력받아

해당 괄호에 각각 2, 3 의 값을 부여해 괄호의 위치에따라 * , + 연산을 해주어 결과값을 도출해내는 문제

 

예제 1번을 보자면

1.

( ( ) [ [ ] ] ) ( [ ] )
  2   3       3  

2.

( 2 [ 3 ] ) ( 3 )
    3 * 3   2 * 3

3.

( 2 9 ) 6
( 2 + 9 ) * 2  

4.

22 6
22 + 6

으로

결과값 28 이 나오겠다.

 

이전 단계의 괄호문제를 풀었더라면 고려해야할점이 쉽게 나올것 이다.

 

1. 문자들을 쪼개어 관리

 

2. 쪼갠 문자마다 처리

 

3. 결과 리턴

 

그럼 코드를 구성해보자

 

입력

public class training1 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        String string=st.nextToken();
        Stack<Character> stack = new Stack<>();
        int temp = 1; //기본값 해당 수로 계산을 진행
        int sum = 0; //결과값
    }
}

 

조건

for (int i = 0; i < string.length(); i++) {
            switch (string.charAt(i)) {

            case '(':
                stack.push('(');
                temp *= 2;
                break;
            case '[':
                stack.push('[');
                temp *= 3;
                break;
            case ')':
                if (stack.isEmpty() || stack.peek() != '(') {
                    sum = 0;
                    break;
                }

                if (string.charAt(i - 1) == '(') 
                    sum += temp; 
                    stack.pop();
                    temp /= 2;
                    break;
                
            case ']':
                if (stack.isEmpty() || stack.peek() != '[') {
                    sum = 0;
                    break;
                }

                if (string.charAt(i - 1) == '[') 
                    sum += temp; 
                    stack.pop();
                    temp /= 3;
                    break;
                

            }   
        }

입력받은 문자를 쪼개어 각 상황마다 조건을 걸어주었다.

 

출력

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

 

코드전문

package training2;

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

public class training1 {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        String string = st.nextToken();
        Stack<Character> stack = new Stack<>();
        int temp = 1; // 기본값 해당 수로 계산을 진행
        int sum = 0;
        for (int i = 0; i < string.length(); i++) {
            switch (string.charAt(i)) {

            case '(':
                stack.push('(');
                temp *= 2;
                break;
            case '[':
                stack.push('[');
                temp *= 3;
                break;
            case ')':
                if (stack.isEmpty() || stack.peek() != '(') {
                    sum = 0;
                    break;
                }

                if (string.charAt(i - 1) == '(') 
                    sum += temp; 
                    stack.pop();
                    temp /= 2;
                    break;
                
            case ']':
                if (stack.isEmpty() || stack.peek() != '[') {
                    sum = 0;
                    break;
                }

                if (string.charAt(i - 1) == '[') 
                    sum += temp; 
                    stack.pop();
                    temp /= 3;
                    break;
                

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

 

결과

수정후 보완

'백준 알고리즘' 카테고리의 다른 글

39. 1743번 음식물 피하기  (0) 2021.11.04
38. 2667번 단지번호붙이기  (0) 2021.11.04
33. 1963번 소수 경로  (0) 2021.10.18
32. 1916번 최소비용 구하기  (0) 2021.10.18
30. 14502번 연구소  (0) 2021.10.18