알고리즘/백준 BOJ
[BOJ C#] 괄호 9012 _case1
왹져박사
2023. 1. 11. 17:50
처음에 이해를 잘못해서 출력은 맞지만 문제의 의도와는 다르게 푼 것 같습니다~~!! 참고만 해주세요
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VPS
{
class App
{
Stack<char> stack = new Stack<char>();
//생성자
public App()
{
Console.WriteLine("App");
//case 1
string[] arr = {
"(())())",
"(((()())()",
"(()())((()))",
"((()()(()))(((())))()",
"()()()()(()()())()",
"(()((())()("
};
for (int i = 0; i < arr.Length; i++)
{
StringToChar(arr[i]);
Console.WriteLine(Vps(arr[i]));
stack.Clear();
}
}
void StringToChar(string vps) //확인
{
for (int i = 0; i < vps.Length; i++)
{
stack.Push(vps[i]);
}
}
string Vps(string vps)
{
char open = '(';
int countOpen = 0;
int countClose = 0;
for(int i = 0; i < vps.Length; i++)
{
if (stack.Pop() == open)
{
countOpen++;
}
else
{
countClose++;
}
}
if (countOpen!=countClose)
{
return "NO";
}
else
{
return "YES";
}
}
}
}
실행 결과
