[백준] 균형잡힌 세상

jun·2021년 3월 28일
0
post-thumbnail

유의할점

전형적인 스택유형

균형이 안잡히는 경우

(가 많은 경우 → 처음에 검증

)가 많은 경우 → 나중에 검증

풀이

괄호 아닌 문자는 무시. 전형적인 스택유형으로 푼다.

코드

C++


#include <iostream>
#include <stack>
#include <string>

using namespace std;

//'('
//'['
int main() {
	stack<char> st;
	while (1) {
		string s;
		getline(cin, s);
		if (s == ".")
			return 0;
		bool flag = true;
		for (int i = 0; i < s.size(); i++) {
			if (s[i] == '(' || s[i] == '[')
				st.push(s[i]);
			else if (s[i] == ')' || s[i] == ']') {
				if (!st.empty() && (st.top() == s[i] - 1 || st.top() == s[i] - 2))
					st.pop();
				else {
					flag = false;
					break;
				}
			}
		}
		if (!st.empty() && !(flag = false))
			while (!st.empty())
				st.pop();
		flag ? cout << "yes" : cout << "no";
		cout << "\n";
	}
}
profile
Computer Science / Algorithm / Project / TIL

0개의 댓글