题解

梁煜然  •  1个月前


#include<bits/stdc++.h>
using namespace std;
int main(){
	stack<char>st;
	string s;
	cin>>s;
	for(int i=0;i<s.size();i++){
		if(s[i]=='('||s[i]=='['){
			st.push(s[i]);
		}else if(s[i]==')'){
			if(st.top()=='('){
				st.pop();
			}else{
				printf("NO"),exit(0);
			}
		}else if(s[i]==']'){
			if(st.top()=='['){
				st.pop();
			}else{
				printf("NO"),exit(0);
			}
		}
	}
	printf("%s",st.empty()?"YES":"NO");
} 

评论: