알고리즘

C++ 알고리즘 - 백준 4949 균형잡힌 세상

마루설아 2024. 12. 31. 18:42
#include <iostream>
#include <stack>
#include <string>
#define endl "\n"

using namespace std;

int main(void) {
	// C++ Init
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	string str = "";
	char ch;

	while (true) {
		stack<char> st;

		getline(cin, str);
		if (str == ".") return 0;

		for (int i = 0; i < str.size(); i++) {
			if (str[i] != '(' && str[i] != ')' && str[i] != '[' && str[i] != ']' && str[i] != '.') continue;
			
			if (str[i] == '(' || str[i] == '[') {
				st.push(str[i]);
				continue;
			}

			if (str[i] == ')' || str[i] == ']') {
				if (st.empty()) {
					cout << "no" << endl;
					st = {};
					break;
				}
				
				char ch = st.top();
				st.pop();

				if (str[i] == ')' && ch != '(') {
					cout << "no" << endl;
					st = {};
					break;
				}

				if (str[i] == ']' && ch != '[') {
					cout << "no" << endl;
					st = {};
					break;
				}
			}

			if (i == str.size() - 1) {
				if (str[i] == '.' && st.empty()) {
					cout << "yes" << endl;
					st = {};
				}

				else {
					cout << "no" << endl;
					st = {};
				}
			}
		}

		if (!st.empty()) {
			cout << "no" << endl;
			st = {};
		}
	}
}