[4949] 균형잡힌 세상

RudinP·2023년 4월 25일
0

BaekJoon

목록 보기
61/77

생각

  1. 문자열을 스택에 넣는다.
  2. 스택에서 TryPop을 한다
  • 띄어쓰기거나 .이거나 영어문자라면 before 변수에 저장하고 continue
  • 괄호라면 before 값과 TryPeek 한 값이 둘 다 영문자라면 문자균형이 아니므로 false break
    - ],)면 괄호용 별도의 스택에 푸시한다.
    - (,[면 괄호스택에서 TryPop을 하여 각자의 짝이 맞으면 continue, 아니라면 false break
  1. 괄호스택이 비어있지 않다면 괄호균형이 아니므로 false

처음 코드

namespace SongE
{
    public class Program2
    {
        static void Main(string[] args)
        {
            using var input = new System.IO.StreamReader(Console.OpenStandardInput());

            //int intInput() => int.Parse(input.ReadLine());
            //int[] intsInput() => Array.ConvertAll(input.ReadLine().Split(), s => int.Parse(s));
            string s;
            while ((s = input.ReadLine()) != ".")
            {
                IsBalanced(s.ToCharArray());
            }
        }

        static void IsBalanced(char[] chars)
        {
            using var output = new System.IO.StreamWriter(Console.OpenStandardOutput());

            Stack<char> stack = new Stack<char>(chars);
            Stack<char> pStack = new();
            bool isBalanced = true;

            char before = ' ';

            while (stack.TryPop(out char c))
            {
                if (char.IsWhiteSpace(c) || c == '.' || char.IsLetter(c))
                {
                    before = c;
                    continue;
                }
                else //[()]
                {
                    // 문자균형
                    if (stack.TryPeek(out char t))
                    {
                        if (char.IsLetter(t) && char.IsLetter(before))
                        {
                            isBalanced = false;
                            break;
                        }
                    }

                    before = c;
                    if (c == ']' || c == ')')
                    {
                        pStack.Push(c); continue;
                    }
                    else
                    {
                        if (pStack.TryPop(out char p))
                        {
                            if (c == '[' && p == ']' || c == '(' && p == ')')
                            {
                                continue;
                            }

                        }

                        isBalanced = false;
                        break;

                    }
                }

            }
            if(pStack.Count > 0) isBalanced = false;
            output.WriteLine(isBalanced ? "yes" : "no");
        }

    }
}

알고보니까 단어가 잘리는건 상관이 없었다

두번째 코드

namespace SongE
{
    public class Program2
    {
        static void Main(string[] args)
        {
            using var input = new System.IO.StreamReader(Console.OpenStandardInput());

            //int intInput() => int.Parse(input.ReadLine());
            //int[] intsInput() => Array.ConvertAll(input.ReadLine().Split(), s => int.Parse(s));
            string s;
            while ((s = input.ReadLine()) != ".")
            {
                IsBalanced(s.ToCharArray());
            }
        }

        static void IsBalanced(char[] chars)
        {
            using var output = new System.IO.StreamWriter(Console.OpenStandardOutput());

            Stack<char> stack = new Stack<char>(chars);
            Stack<char> pStack = new();
            bool isBalanced = true;

            while (stack.TryPop(out char c))
            {
                if (char.IsWhiteSpace(c) || c == '.' || char.IsLetter(c))
                {
                    continue;
                }
                else //[()]
                {
                    if (c == ']' || c == ')')
                    {
                        pStack.Push(c); continue;
                    }
                    else
                    {
                        if (pStack.TryPop(out char p))
                        {
                            if (c == '[' && p == ']' || c == '(' && p == ')')
                            {
                                continue;
                            }

                        }

                        isBalanced = false;
                        break;

                    }

                }

            }
            if (pStack.Count > 0) isBalanced = false;
            output.WriteLine(isBalanced ? "yes" : "no");
        }

    }
}

정리본

namespace SongE
{
    public class Program2
    {
        static void Main(string[] args)
        {
            using var input = new System.IO.StreamReader(Console.OpenStandardInput());
            using var output = new System.IO.StreamWriter(Console.OpenStandardOutput());

            //int intInput() => int.Parse(input.ReadLine());
            //int[] intsInput() => Array.ConvertAll(input.ReadLine().Split(), s => int.Parse(s));
            string s;
            while ((s = input.ReadLine()) != ".")
            {
                output.WriteLine(IsBalanced(s.ToCharArray()) ? "yes" : "no");
            }
        }

        static bool IsBalanced(char[] chars)
        {

            Stack<char> stack = new();

            foreach(char c in chars)
            {
                if (char.IsLetter(c) || c == ' ' || c == '.')
                    continue;
                else
                {
                    if(c == '[' || c == '(')
                        stack.Push(c);
                    else
                    {
                        if (stack.TryPop(out char t))
                        {
                            if (t == '[' && c == ']' || t == '(' && c == ')')
                                continue;
                            else
                            {
                                return false;
                            }
                        }
                        else
                            return false;
                    }
                }
            }

            return stack.Count > 0 ? false : true;
        }

    }
}

profile
곰을 좋아합니다. <a href = "https://github.com/RudinP">github</a>

0개의 댓글