나의 브을로오그으

[c++] 4949번 : 균형잡힌 세상 본문

알고리즘/BaekJoon

[c++] 4949번 : 균형잡힌 세상

__jhp_+ 2022. 4. 30. 16:21

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

 

4949번: 균형잡힌 세상

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

www.acmicpc.net

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

#define NO			0
#define YES			1
bool isCheckPair(char c1, char c2)
{
	if (c1 == '(' && c2 == ')') return true;
	if (c1 == '[' && c2 == ']') return true;
	return false;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	stack<char> st;
	const char* result[2] = { "no", "yes"};
	string buffer;
	while (true)
	{
		while (!st.empty()) { st.pop(); }
		getline(cin, buffer);
		if (buffer.length() == 1 && buffer[0] == '.') break;
		int flag = YES;
		for(int i = 0 ; i < buffer.length(); ++i)
		{
			if (buffer[i] == '(' || buffer[i] == '[')
			{
				st.push(buffer[i]);
			}
			else if (buffer[i] == ')' || buffer[i] == ']')
			{
				if (st.empty() == true)
				{
					flag = NO;
					break;
				}
				else
				{
					if (isCheckPair(st.top(), buffer[i]) == true)
					{
						st.pop();
					}
					else
					{
						flag = NO;
						break;
					}
				}
			}
		}
		if (st.empty() == false) flag = NO;
		cout << result[flag] << '\n';
	}
	return 0;
}

'알고리즘 > BaekJoon' 카테고리의 다른 글

[c++] 9012번 : 괄호  (0) 2022.05.04
[c++] 7568번 : 덩치  (0) 2022.05.04
[c++] 4153번 : 직각삼각형  (0) 2022.04.30
[c++] 2869번 : 달팽이는 올라가고 싶다  (0) 2022.04.30
[c++] 2839번 : 설탕 배달  (0) 2022.04.29