algorithm/BOJ

BOJ 10799번 쇠막대기

_JunHo 2020. 1. 11. 15:28

BOJ : https://www.acmicpc.net/problem/10799

 

쇠막대기 자르기 문제이다.

( ) 모양이 되면 막대를 자르면서 갯수를 추가해주면 된다.

( ), ( (, ) (, ) ) 이 4가지 모양을 보면 규칙을 찾을 수 있다.

 

더보기
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
#include <iostream>
#include <string>
#include <stack>
using namespace std;
 
stack<char> s;
 
int main() {
    string str;
    cin >> str;
    for (int i = str.size() - 1; i >= 0; i--) {
        s.push(str[i]);
    }
 
    int total = 0;
    int open = 1;
    char pre = '(', now;
    s.pop();
    while (!s.empty()) {
        now = s.top();
        s.pop();
 
        if (now == '(') {
            open++;
        }
        if (now == ')') {
            if (pre == '(') {
                open--;
                total += open;
            }
            if (pre == ')') {
                total++, open--;
            }
        }
 
        pre = now;
    }
    cout << total;
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter