SMALL

https://www.acmicpc.net/problem/9012

 

9012번: 괄호

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

www.acmicpc.net


  • 문제


  • 문제풀이

문장을 char형태로 받은 후 다시 배열로 받아 문자열별로 판단하여 Push와 Pop을 진행하였다.

 


  • 코드 1
using System.Collections.Generic;
using System;
using System.Collections;
using System.Text;
using System.Diagnostics.CodeAnalysis;

namespace yongyong2
{
    internal class Program
    {
       
        public static void Main(string[] args)
        {
            Stack<string> stack = new Stack<string>();
            int input = int.Parse(Console.ReadLine());
            string[] output= new string[input];

            for (int i=0;i<input;i++)
            {
                string sentence = Console.ReadLine();
                char[] arr= sentence.ToCharArray();
                stack.Clear();
                
                for(int j=0;j<arr.Length;j++)
                {
                    if (arr[j] == '(')
                    {
                        stack.Push("(");
                    }
                    else if (sentence[j] == ')')
                    {

                        if (stack.Count != 0)
                        {
                            stack.Pop();
                            output[i] = "YES";
                        }
                        else
                        {
                            output[i] = "NO";
                            break;
                        }
                    }
                    if (stack.Count != 0)
                    {
                        output[i] = "NO";
                    }
                }
                
            }
            for(int i=0;i<input;i++)
            {
                Console.WriteLine(output[i]);
            }
            

            

        }

    }
}

  • 후기

문장을 받을때는 char배열로 받은후 배열로 바꿔주면 됨을 알 수 있었다.

LIST

'백준 > C#' 카테고리의 다른 글

[C#]백준 11866번: 요세푸스 문제 0  (0) 2023.01.28
[C#]백준 2164번: 카드2  (0) 2023.01.28
[C#]백준 10773번: 제로  (0) 2023.01.05
[C#]백준 2775번: 부녀회장이 될테야  (0) 2023.01.05
[C#]백준 10828번: 스택  (0) 2023.01.05

+ Recent posts