Notice
Recent Posts
Recent Comments
«   2025/08   »
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
Today
Total
Archives
관리 메뉴

cyphen156

백준-스택, 큐, 덱 1 9012 괄호 본문

컴퓨터공학/알고리듬 풀이

백준-스택, 큐, 덱 1 9012 괄호

cyphen156 2025. 6. 10. 12:09

괄호

올바르지 않은 괄호 쌍이 존재하는지 아닌지를 판단해서 YES와 NO로 출력하라.

제약사항

  • Correct : (())
  • InCorrect : )(()(), (()
  • 2 <= strlen <= 50

주의 사항

저처럼 stdio 헤더랑 iostream 같이 쓰면 오답처리되니 하지마세요

 

CPP풀이

괄호_9012.cpp

/**
 * 백준 괄호_9012
 * 올바르지 않은 괄호 쌍이 존재하는지 아닌지를 판단해서 YES와 NO로 출력하라.
 * 
 * 제한사항
 *****************************************
 * Correct : (())                        *
 * InCorrect : )(()(), (()               *
 *****************************************
 *
 *
 *
 * 주의
 * 저처럼 stdio 헤더랑 iostream 같이 쓰면 오답처리되니 하지마세요 * 
 * 
 * 풀이시간 5분
 */


#include <iostream>
#include <stdio.h>

using namespace std;

int main(void)
{
    int T;
    cin >> T;

    for (int i = 0; i < T; ++i)
    {
        char str[51];
        scanf("%s", str);
        
        int j = 0;
        int size = 0;
        bool isVPS = true;

        while(str[j] != '\0')
        {
            if (str[j] == '(')
            {
                size++;
            }
            else if(str[j] == ')')
            {
                size--;
                
                if (size < 0)
                {
                    isVPS = false;
                    break;
                }
            }
            j++;
        }

        if (size != 0)
        {
            isVPS = false;
        }

        if (isVPS)
        {
            cout << "YES" << '\n';
        }
        else 
        {
            cout << "NO" << '\n';
        }
    }
    return 0;
}

모든 예제 코드의 소스파일은 제 개인 깃허브 레포지토리에 있습니다.

 

Workspace/알고리듬 풀이 at main · cyphen156/Workspace

Studying . Contribute to cyphen156/Workspace development by creating an account on GitHub.

github.com