하다보니

3986번-균형 잡힌 세상 본문

알고리즘 풀이/백준

3986번-균형 잡힌 세상

claire 2022. 3. 7. 15:05
#include<iostream>
#include<string>
#include<stack>
using namespace std;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	while (true) {
		stack<char> s;
		bool isValid = true;
		string a;
		getline(cin, a);
		if (a == ".")break;
		for (auto c : a) {
			if (c == '(' || c == '[')s.push(c);
			else if (c == ')') {
				if (s.empty() || s.top() != '(') {
					isValid = false;
					break;
				}
				s.pop();
			}
			else if (c == ']') {
				if (s.empty() || s.top() != '[') {
					isValid = false;
					break;
				}
				s.pop();
			}
		}
		if (!s.empty())isValid = false;
		if (isValid)cout << "yes\n";
		else cout << "no\n";
	}
	
}

'알고리즘 풀이 > 백준' 카테고리의 다른 글

n과 m(1)  (0) 2022.03.08
3986번-좋은 단어  (0) 2022.03.07
2630번-색종이 만들기  (0) 2022.02.18
6593번-상범 빌딩  (0) 2022.02.17
1780번-종이의 개수  (0) 2022.02.16