文章

119

粉丝

0

获赞

166

访问

10.9k

头像
括号匹配 题解:
P1501 西北工业大学机试题
发布于2026年1月26日 20:39
阅读数 77

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

string str;
stack<char> st;

bool check()
{
	for(int i=0;i<str.size();i++)
	{
		if(str[i]=='('||str[i]=='[')
			st.push(str[i]);
		else
		{
			if(st.empty())
				return false;
			if(str[i]==']'&&st.top()!='[')
				return false;
			if(str[i]==')'&&st.top()!='(')
				return false;
			st.pop();
		}
	}
	if(!st.empty())
		return false;
	return true;
}
int main()
{
	cin>>str;
	bool flag=check();
	if(flag)
		cout<<"YES";
	else
		cout<<"NO";
	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发