#include <bits/stdc++.h>
using namespace std;
char _string[60];
stack<char>S;
int push_pop(char _string[]);
int main()
{
while(gets(_string))
{
while(!S.empty())
{
S.pop();
}//记得清空栈,连交几次wa,这就比较尴尬了
if(push_pop(_string))
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
}
return 0;
}
int push_pop(char _string[])
{
int lenth = strlen(_string);
for(int i = 0; i < lenth; i++)
{
switch(_string[i])
{
case '(':
case '[':
case '{':S.push(_string[i]);break;
case ')':if(!S.empty()&&S.top() == '(') S.pop();
else return 0;break;
case ']':if(!S.empty()&&S.top() == '[') S.pop();
else return 0;break;
case '}':if(!S.empty()&&S.top() == '{') S.pop();
else return 0;break;
}
}
if(S.empty())//栈空
return 1;
return 0;
}
SDUT 2143 数据结构实验之栈四:括号匹配
最新推荐文章于 2020-05-18 13:00:32 发布