SDJ( 수돈재 아님 ㅎ )

[C++] 9012 - 괄호 본문

알고리즘/Backjoon

[C++] 9012 - 괄호

ShinDongJun 2020. 1. 11. 12:39

문제 링크 : https://www.acmicpc.net/problem/9012

 

9012번: 괄호

문제 괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고 부른다. 한 쌍의 괄호 기호로 된 “( )” 문자열은 기본 VPS 이라고 부른다. 만일 x 가 VPS 라면 이것을 하나의 괄호에 넣은 새로운 문자열 “(x)”도 VPS 가 된다. 그리고 두 VPS x 와 y를 접합(conc

www.acmicpc.net

 

 

 

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
#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#define endl '\n'
 
using namespace std;
 
bool M(const string& S)
{
    const string opening("("), closing(")");
 
    stack<char> Stack;
 
    for(int i = 0; i < S.size(); ++i)
    {
        if(opening.find(S[i]) != -1)
            Stack.push(S[i]);
        else
        {
            if(Stack.empty())    return false;
            Stack.pop();
        }
    }
 
    return Stack.empty();
}
 
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    string K;
    int n;
    cin >> n;
 
    while(n--)
    {
        cin >> K;
        if(M(K))
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
 
 
    return 0;
}

'알고리즘 > Backjoon' 카테고리의 다른 글

[C++] 1011 - Fly me to the Alpha Centauri  (0) 2020.01.11
[C++] 4949 - 균형잡힌 세상  (0) 2020.01.11
[C] 1932 - 정수 삼각형  (0) 2020.01.11
[Python3] 2193 - 이친수  (0) 2020.01.11
[C] 1149 - RGB거리  (0) 2020.01.11
Comments