[BOJ/C#] 4949 균형잡힌 세상

Algorithm 2023. 1. 26. 23:34
반응형

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

 

4949번: 균형잡힌 세상

하나 또는 여러줄에 걸쳐서 문자열이 주어진다. 각 문자열은 영문 알파벳, 공백, 소괄호("( )") 대괄호("[ ]")등으로 이루어져 있으며, 길이는 100글자보다 작거나 같다. 각 줄은 마침표(".")로 끝난다

www.acmicpc.net

 

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;

namespace Assignment01
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(new BufferedStream(Console.OpenStandardInput()));
            StreamWriter sw = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()));
            string ans = "";
            
            while (true)
            {
                string str = sr.ReadLine();
                
                if (str.Length == 1 && str[0] == '.')
                {
                    break;
                }

                bool isYes = true;
                
                Stack<char> stack = new Stack<char>();
                
                foreach (var x in str)
                {
                    if(x == '(' || x == '[') 
                        stack.Push(x);
                    else if( x == ')')
                    {
                        if (stack.TryPeek(out char result) && result == '(')
                            stack.Pop();
                        else
                        {
                            isYes = false;
                            break;;
                        }
                    }else if (x == ']')
                    {
                        if (stack.TryPeek(out char result) && result == '[')
                            stack.Pop();
                        else
                        {
                            isYes = false;
                            break;;
                        }
                    }
                }
                
                //Console.WriteLine("{0} {1}", isYes, stack.Count);

                if (isYes && stack.Count <=0)
                {
                    ans = "yes";
                }
                else
                {
                    ans = "no";
                }
                
                sw.WriteLine(ans);
            }
            
            sr.Close();
            sw.Close();
        }
    }
}
반응형

'Algorithm' 카테고리의 다른 글

[BOJ/C#] 2579 계단오르기  (0) 2023.01.28
[BOJ/C#] 15829 Hashing  (0) 2023.01.27
[BOJ/C#] 11724 연결 요소의 개수  (0) 2023.01.26
이분탐색 과 Lower Bound , Upper Bound (상/하한선)  (0) 2023.01.26
[BOJ/C#] 10250 ACM 호텔  (0) 2023.01.26
: