文章

77

粉丝

1

获赞

295

访问

5.7k

头像
括号匹配 题解:
P1501 西北工业大学机试题
发布于2026年3月15日 21:59
阅读数 43

#include <iostream>
#include <stack>
using namespace std;

int main(){
	string s;
	cin >> s;
	
	stack<char> st;
	
	for(int i=0;i<s.size();i++){
		if(s[i]=='(' || s[i]=='['){
			st.push(s[i]);
		}
		else{
			if(st.empty()){
				cout << "NO";
				return 0;
			}
			
			char t = st.top();
			st.pop();
			
			if(s[i]==')' && t!='('){
				cout << "NO";
				return 0;
			}
			if(s[i]==']' && t!='['){
				cout << "NO";
				return 0;
			}
		}
	}
	if(st.empty())
		cout << "YES";
	else
		cout << "NO";
	
	return 0;
}
			
			
				

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发