ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 스택_백준_괄호의 값_2504
    Algorithm 2018. 8. 15. 13:53


    for문으로 탐색 하면서 열린 괄호가 나올 경우 스택에 넣고 닫힌 괄호가 나올 경우 연속된 괄호열 중 최초의 결합된 괄호로부터 분배법칙으로 계산을 한다. 예를들어 (([])())경우 최초의 결합은 []이다. 이 경우 2*3이다. 이 계산은 2*((2*3)+2) = 2*(2*3) + 2*2로 계산을 할 수 있다.


    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
    89
    #include <iostream>
    #include <stack>
    #include <string>
    using namespace std;
     
    int main() {
        string input;
        stack <char> s;
        stack <char> tmp;
        int result = 0;
        int value = 0;
        cin >> input;
     
        s.push('-1'); // protect stack(empty) top access error
     
        for (int i = 0; i < input.size(); i++) {
            if (input[i] == '(') {
                s.push('(');
            }
            else if (input[i] == '[') {
                s.push('[');
            }
     
            else if (input[i] == ')') {
                if (s.top() != '(') {
                    cout << 0 << endl;
                    return 0;
                }//Not correct
     
                if (input[i - 1== '(') {
                    s.pop();
                    tmp = s;
                    value = 2;
                    while (!tmp.empty()) {
                        if (tmp.top() == '(') {
                            value = 2 * value;
                        }
                        if (tmp.top() == '[') {
                            value = 3 * value;
                        }
                        tmp.pop();
                    }
                    result += value;
                } // Calculate
     
                else {
                    s.pop();
                }
            }
            else if (input[i] == ']') {
                if (s.top() != '[') {
                    cout << 0 << endl;
                    return 0;
                }//Not correct
                if (input[i - 1== '[') {
                    s.pop();
                    tmp = s;
                    value = 3;
                    while (!tmp.empty()) {
                        if (tmp.top() == '(') {
                            value = 2 * value;
                        }
                        if (tmp.top() == '[') {
                            value = 3 * value;
                        }
                        tmp.pop();
                    }
                    result += value;
                }// Calculate
     
                else {
                    s.pop();
                }
            }
            else {
                cout << "Error" << endl;
            } // Input Error
        }
     
        s.pop();
        if (s.empty()) {
            cout << result << endl;
        }
        else {
            cout << 0 << endl;
        }// Not correct
        return 0;
    }
    // First, judge whether it is correct.If it is correct, judge separately by distribution law.
    cs


    'Algorithm' 카테고리의 다른 글

    스택_백준_키로거_5397  (0) 2018.08.15
    스택_백준_탑_2493  (0) 2018.08.15
    스택_백준_스택 수열_1874  (0) 2018.08.14
    스택_백준_에디터_1406  (0) 2018.08.14
    스택_백준_쇠막대기_10799  (0) 2018.08.14
Designed by Tistory.