자료구조 스택을 활용하는 문제
열린괄호를 만났을 때는 스택에 push
닫힌괄호를 만났을 때는 스택 맨 위 값이 닫힌괄호의 유형과 같은 열린괄호라면
짝이 제대로 맞다는 것을 확인 가능
그렇지 않다면 짝이 맞지 않음
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <stack>
//백준 4949, 균형잡힌 세상
int main(){
while(true){
std::string s;
std::getline(std::cin, s);
if(s == ".") break;
std::stack<char> strings;
bool flag = true;
for(auto c : s){
if(c == '[' || c == '(') strings.push(c);
else if(c == ']'){
if(strings.empty() || strings.top() != '['){
flag = false;
break;
}
strings.pop();
}
else if(c == ')'){
if(strings.empty() || strings.top() != '('){
flag = false;
break;
}
strings.pop();
}
}
if(!strings.empty()) flag = false;
if(flag) std::cout << "yes\n";
else std::cout << "no\n";
}
return 0;
}