Baekjoon

[#9012] 괄호

강람이 2020. 3. 15. 14:27
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
#include <iostream>
#include <stack>
#include <string>
#include <vector>
 
using namespace std;
 
int main() {
    int t;
    cin >> t;
 
    vector<bool> result(t, true);
 
    for (int tc = 0; tc < t; tc++) {
        string str;
        cin >> str;
 
        stack<char> s;
 
        for (int i = 0; i < str.size(); i++) {
            if (str.at(i) == '(') {
                //열린 괄호면
                s.push('(');  //push
            }
            else {
                //닫힌 괄호면
                if (s.empty()) {  
                    //stack이 비어있다는 것은 (보다 )를 더 많이 만났다는 뜻이므로 VPS가 아님
                    result[tc] = false;
                    break;
                }
                else {
                    s.pop();
                }
            }
        }
 
        if (!s.empty()) {
            //남아있는 ( 있으면 VPS 아님
            result[tc] = false;
        }
    }
 
    for (int i = 0; i < t; i++) {
        if (result[i])
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

( 만나면 stack에 push, ) 만나면 stack에서 pop

pop 해야 되는데 stack에 데이터 없으면 VPS 아님

문자열 끝까지 다 봤는데 stack에 데이터 남아있으면 VPS 아님