#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;
}
SWUST数据结构--括号匹配(简便已AC)
最新推荐文章于 2020-04-22 11:22:23 发布