본문 바로가기
Algorithm/C++

[백준 9012] 괄호

by imagineer_jinny 2022. 4. 8.

9012번: 괄호 (acmicpc.net)

 

9012번: 괄호

괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고

www.acmicpc.net

#include <iostream>
#include <string>
using namespace std;
string valid(string s) {
    int cnt = 0;
    for (int i=0; i<s.size(); i++) {
        if (s[i] == '(') {
            cnt += 1;
        } else {
            cnt -= 1;
        }
        if (cnt < 0) {
            return "NO";
        }
    }
    if (cnt == 0) {
        return "YES";
    } else {
        return "NO";
    }
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        string s;
        cin >> s;
        cout << valid(s) << '\n';
    }
    return 0;
}

 

'Algorithm > C++' 카테고리의 다른 글

[백준 1874번] 스택 수열  (0) 2022.04.10
[백준 5635번] 생일  (0) 2022.04.09
[백준 9093] 단어 뒤집기  (0) 2022.04.07
[프로그래머스 lv2] 피보나치 수  (0) 2022.03.30
[이코테 2021] 음료수 얼려먹기 / DFS  (0) 2021.09.29

댓글