[백준 4949] 균형잡힌 세상

alsry._.112·2023년 10월 5일
0

백준

목록 보기
75/102

🔗문제 풀러가기
단계별로 풀어보기 단계 16의 4번째 문제이다.

문제 분석


Stack 컨테이너를 사용해서 문제를 해결하였다.

코드

#include <iostream>
#include <string>
#include <stack>
using namespace std;

int main()
{
	string input;
	while (true)
	{
		getline(cin, input);
		stack<char> st;
		if (input[0] == '.') break;

		for (int i = 0; i < input.length() - 1; i++)
		{
			if (input[i] == '(') st.push('(');
			if (input[i] == '[') st.push('[');

			if (input[i] == ')')
			{
				if (!st.empty() && st.top() == '(') { st.pop(); }
				else
				{
					cout << "no" << endl;
					break;
				}
			}

			if (input[i] == ']')
			{
				if (!st.empty() && st.top() == '[') { st.pop(); }
				else
				{
					cout << "no" << endl;
					break;
				}
			}

			if (st.empty() && i == input.length() - 2) cout << "yes" << endl;
			else if (!st.empty() && i == input.length() - 2) cout << "no" << endl;
		}

	}
}
profile
소통해요

0개의 댓글