[백준_Stack]2504번_괄호의 값

Posted by 열정보이
2018. 11. 6. 12:53 Algorithms

9012번 괄호 문제와 비슷하지만 조금 더 복잡한 문제라고 할 수 있다.



하지만 이러한 문제도 stack을 이용한다면 쉽게 풀 수 있다.


예를들어 (()[[]]) 를 보게 되면 아래 그림과 같이 진행된다고 생각하고 풀이를 진행하면 된다.



이 문제를 풀때 중요한 건 괄호 안의 값은 결국 괄호값 만큼 곱해줘야 한다는 것이다.


그렇다면 코드를 바로 보도록 하자


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
 
public class Al2504_RE{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        Stack<String> stack = new Stack<>();
        
        /* 문자열의 길이가 홀수일 경우 정답이 될 수 없다. */
        if(str.length() %2 != 0) {
            System.out.println(0);
            return;
        }
        
        for (int i = 0; i < str.length(); i++) {
            String temp = String.valueOf(str.charAt(i));
            
            if(temp.equals("("|| temp.equals("[")) {
                stack.push(temp);
            }
            else if(temp.equals(")")){
                if(stack.isEmpty()) {
                    System.out.println(0); return;
                }
                /* 괄호 사이에 동일 괄호 외 아무것도 존재하지 않을 경우 */
                if(stack.peek().equals("(")) {
                    stack.pop();
                    stack.push("2");
                }
                /* 괄호 사이에 다른 괄호값(숫자)가 존재 할 경우 */
                else {
                    int sum = 0;
                    while(!stack.isEmpty()) {
                        String temp2 = stack.pop();
                        if(temp2.equals(")"|| temp2.equals("["|| temp2.equals("]")){
                            System.out.println(0); return;
                        }
                        /* 괄호가 나오면 지금까지 더했던 값들을 *2 하여 다시 push */
                        if(temp2.equals("(")) {
                            stack.push(String.valueOf(sum*2));
                            break;
                        }
                        sum += Integer.parseInt(temp2);
                    }
                }
            }
            else if(temp.equals("]")){
                if(stack.isEmpty()) {
                    System.out.println(0); return;
                }
                /* 괄호 사이에 동일 괄호 외 아무것도 존재하지 않을 경우 */
                if(stack.peek().equals("[")) {
                    stack.pop();
                    stack.push("3");
                }
                /* 괄호 사이에 다른 괄호값(숫자)가 존재 할 경우 */
                else {
                    int sum = 0;
                    while(!stack.isEmpty()) {
                        String temp2 = stack.pop();
                        if(temp2.equals(")"|| temp2.equals("("|| temp2.equals("]")){
                            System.out.println(0); return;
                        }
                        /* 괄호가 나오면 지금까지 더했던 값들을 *3 하여 다시 push */
                        if(temp2.equals("[")) {
                            stack.push(String.valueOf(sum*3));
                            break;
                        }
                        sum += Integer.parseInt(temp2);
                    }
                }
            }
        }
        int ans = 0;
        while(!stack.isEmpty()) {
            String temp3 = stack.pop();
            if(temp3.equals("("|| temp3.equals("["|| temp3.equals(")"|| temp3.equals("]")) {
                System.out.println(0);
                return;
            }
            ans += Integer.parseInt(temp3);
        }
        System.out.println(ans);
    }
}
cs

9012 괄호와 다른점은 Stack에 괄호만 저장하는 것이 아닌, 값도 저장해서 괄호 사이의 값을 더해주는 과정이 추가되었다는 점인 것 같다.


또 다른 문제도 도전해봐야지!


'Algorithms' 카테고리의 다른 글

[백준_Queue]1966번_프린터 큐  (0) 2018.11.18
[백준_Queue]10845번_큐  (0) 2018.11.13
[백준_Stack]1874번_스택 수열  (0) 2018.11.11
[백준_Stack]9012번_괄호  (0) 2018.11.04
[백준_Stack]10828번_스택  (0) 2018.11.03