SWUST数据结构--括号匹配(简便已AC)

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

stack<char>s;

bool isl(char a)
{
	return ((a == '(')||(a == '['));
}
bool isr(char a)
{
	return ((a == ')')||(a == ']'));
}
bool juge(char a,char b)
{
	return (a == '(' && b == ')')||(a == '[' && b == ']');
}

int main()
{
	char str[100];
	cin>>str;
	int len=strlen(str);
	for(int i=0;i<len;i++)
	{
		if(isl(str[i])) 
		{
			s.push(str[i]);
		}
		if(isr(str[i]))
		{
			if(!s.empty())
			{
				if(!juge(s.top(),str[i]))
				{
					cout<<"NO";
					return 0;
				}
				s.pop();
			}
			else s.push(str[i]);
		}
	}
	if(s.empty()) cout<<"YES";
	else cout<<"NO";
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41681743/article/details/79972155
今日推荐