알고리즘 문제풀이/백준
[백준 9012] 괄호
홍시홍
2020. 5. 28. 19:06
분류
구현
요구사항
주어진 괄호가 정상인지 판단하기
풀이
기본적인 문제다
'('가 들어오면 stack에 넣어주고 ')'가 들어올 경우 top을 확인해서 '('일 경우 NO
'(' 아닐 경우, YES
계산이 끝난 후 스택이 비어있으면 YES 아니면 NO
#include <iostream>
#include <queue>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
int n;
int main(){
scanf("%d",&n);
for(int tc=1;tc<=n;tc++){
int flag=0;
deque<char> st;
string str;
cin>>str;
for(int i=0 ; i <str.size() ; i++){
char now = str[i];
if(now == ')'){
if(st.empty()){
flag=1;
break;
}
else{
char next1 = st.back();
if(next1 == '(')
st.pop_back();
else {
flag=1;
break;
}
}
}
if(now=='('){
st.push_back(now);
}
for(int j=0 ; j <st.size() ; j++){
//cout<<st[j]<<" ";
}
//cout<<endl;
}
if(flag==1){
printf("NO\n");
continue;
}
if(st.empty()) printf("YES\n");
else printf("NO\n");
}
}