-
9012) 괄호 c++카테고리 없음 2020. 11. 17. 10:39
문제 ) www.acmicpc.net/problem/9012
풀이 )
4949번을 풀었다면 쉽게 풀 수 있다. 4949번과 매우 유사하니 설명은 길게 하지 않는다.
만약 이 문제를 잘 모르겠다면 4949번을 먼저 풀어보는걸 추천한다.
코드 )
#include <iostream> #include <stack> using namespace std; int T; int main(void) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> T; while(T-- > 0) { string input; stack<char> s; cin >> input; int size = input.length(); bool flag = false; if(input.length()==1 && input == ".") break; for(int i=0; i<size; i++){ if(input[i] == '(' || input [i] == '[') s.push(input[i]); else if( input[i] == ')' ){ if( !s.empty() && s.top() == '(' ) s.pop(); else { flag = true; break; } } else if( input[i] == ']' ) { if( !s.empty() && s.top() == '[' ) s.pop(); else { flag = true; break; } } } if( s.empty() && !flag) cout << "YES\n"; else cout << "NO\n"; } }