文章

59

粉丝

0

获赞

235

访问

5.0k

头像
括号匹配 题解:natural thinking
P1501 西北工业大学机试题
发布于2026年3月11日 16:40
阅读数 47

#include <bits/stdc++.h>
using namespace std;

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

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发