본문 바로가기
알고리즘/백준 BOJ

[BOJ C#] 괄호 9012 _case1

by 왹져박사 2023. 1. 11.
728x90

처음에 이해를 잘못해서 출력은 맞지만 문제의 의도와는 다르게 푼 것 같습니다~~!! 참고만 해주세요

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VPS
{
    class App
    {
        Stack<char> stack = new Stack<char>();

        //생성자
        public App()
        {
            Console.WriteLine("App");

            //case 1
            string[] arr = {
                "(())())",
                "(((()())()",
                "(()())((()))",
                "((()()(()))(((())))()",
                "()()()()(()()())()",
                "(()((())()("
            };

            for (int i = 0; i < arr.Length; i++)
            {
                StringToChar(arr[i]);
                Console.WriteLine(Vps(arr[i]));
                stack.Clear();
            }
        }

        void StringToChar(string vps)               //확인
        {
            for (int i = 0; i < vps.Length; i++)
            {
                stack.Push(vps[i]);
            }
        }

        string Vps(string vps)
        {
            char open = '(';
            int countOpen = 0;
            int countClose = 0;

            for(int i = 0; i < vps.Length; i++)
            {
                if (stack.Pop() == open)
                {
                    countOpen++;
                }
                else
                {
                    countClose++;
                }
            }

            if (countOpen!=countClose)
            {
                return "NO";
            }
            else
            {
                return "YES";
            }

        }
    }
}

실행 결과

728x90

'알고리즘 > 백준 BOJ' 카테고리의 다른 글

[BOJ C#] 1181 단어 정렬  (0) 2023.01.12
[BOJ C#] 2711 오타맨 고창영  (0) 2023.01.12
[BOJ C#] 10173 니모를 찾아서  (0) 2023.01.12
[BOJ C#] 4458 첫 글자를 대문자로  (0) 2023.01.12
[BOJ C#] 9086 문자열  (0) 2023.01.12