백준 4949 균형잡힌 세상 ⭕

CJB_ny·2023년 1월 20일
0

백준

목록 보기
60/104
post-thumbnail

균형잡힌 세상

이번 문제도 스택을 사용하는 문제이다.

조건체크를 정확하게 안해서 계속 틀리다가 맞았다.

이런식으로 체크를 해주어도 무방하다.

cpp 내코드

#include <iostream>
#include <string>
#include <stack>
#include <algorithm>
using namespace std;
#define endl "\n"

string s;


bool Check(string s)
{
	stack<char> st;
	bool flag = false;

	for (char c : s)
	{
		if (c == '(') st.push(c);
		else if (c == '[') st.push(c);
		else if (c == ')')
		{
			if (st.empty())
			{
				flag = true; break;
			}
			else
			{
				if (st.top() == '(') st.pop();
				else if (st.top() == '[' || st.top() == ']' || st.top() == ')')
				{
					flag = true;
					break;
				}
			}
		}
		else if (c == ']')
		{
			if (st.empty())
			{
				flag = true; break;
			}
			else
			{
				if (st.top() == '[') st.pop();
				else if (st.top() == ']' || st.top() == ')' || st.top() == '(')
				{
					flag = true;
					break;
				}
			}
		}
	}

	if (!st.empty() || flag == true) return false;
	if (st.empty()) return true;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	while (1)
	{
		getline(cin, s);
		if (s.size() == 1 && s[0] == '.') break;
		if (Check(s)) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}
profile
https://cjbworld.tistory.com/ <- 이사중

0개의 댓글