집게사장의 꿈

백준 C# 9012 괄호 본문

기타/백준

백준 C# 9012 괄호

Krapboss 2024. 6. 9. 21:12

괄호의 짝을 판단하는 문제

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

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int iter = int.Parse(Console.ReadLine().Trim());
            Stack<int> stack = new Stack<int>();

            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < iter; i++)
            {
                string str = Console.ReadLine().Trim();
                bool isValid = true;

                stack.Clear();

                foreach (int c in str)
                {
                    if (c == ('('))
                    {
                        stack.Push(c);
                    }
                    else if (c == (')'))
                    {
                        if (stack.Count == 0)
                        {
                            isValid = false;
                            break;
                        }
                        stack.Pop();
                    }
                }

                if (stack.Count > 0) isValid = false;

                stringBuilder.Append(isValid ? "YES\n" : "NO\n");
            }

            Console.WriteLine(stringBuilder.ToString());
        }
    }
}

'기타 > 백준' 카테고리의 다른 글

백준 C# 2475 검증수  (0) 2024.07.04
백준 C# 2231 분해합  (0) 2024.07.04
백준 c# 4949 균형잡힌 세상  (0) 2024.06.09
백준 C# 10773 제로  (0) 2024.06.06
백준 C# 28278 스택 2  (0) 2024.06.06