文章

81

粉丝

0

获赞

275

访问

10.5k

头像
括号匹配 题解:stack
P1501 西北工业大学机试题
发布于2025年3月22日 22:45
阅读数 72

#include<bits/stdc++.h>
using namespace std;
int main(){
	string str;
	cin>>str;
	int n=str.length();
	stack<char>st;
	for(int i=0;i<n;i++){
		if(!st.empty()){
			char now = st.top();
			if(str[i]==')'&&now=='('||now=='['&&str[i]==']'){
				st.pop();
			}else st.push(str[i]);
		}else{
			st.push(str[i]);
		}
	}
	
	if(st.empty())cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	return 0;


}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发