모든 일상

C# 백준 9012번 문제 괄호 본문

코딩 공부/C#

C# 백준 9012번 문제 괄호

통통푸린 2022. 7. 28. 14:30
728x90
반응형

using System;
using System.Collections;
using System.Collections.Generic;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            int cnt = int.Parse(Console.ReadLine());
            string[] result = new string[cnt];
            Stack stack_1 = new Stack(cnt);
            bool error = false;
            for (int i = 0; i < cnt; i++)
            {
                string s = Console.ReadLine();
                stack_1.Clear();
                for(int j = 0; j<s.Length; j++)
                {
                    if (s.Substring(j,1) == "(")
                    {
                        stack_1.Push(s.Substring(j, 1));
                    }
                    else if (s.Substring(j, 1) == ")")
                    {
                        if (stack_1.Count != 0)
                        {
                            stack_1.Pop();
                            result[i] = "YES";
                        }    
                        else
                        {
                            result[i] = "NO";
                            break;
                        }
                    }
                }
                if (stack_1.Count != 0)
                {
                    result[i] = "NO";
                }
            }
            for(int i = 0; i<cnt; i++)
            {
                Console.WriteLine(result[i]);
            }
        }      
    }
}

728x90
반응형