#include <bits/stdc++.h>
#define MAX 1000000
using namespace std;
char st[100];
bool match(char a, char b)
{
if( (a == '(' && b == ')') ||
(a == '{' && b == '}') ||
(a == '[' && b == ']') )
return true;
else
return false;
}
int main()
{
while(gets(st))
{
int t = 1;
stack<char>Q;
for(int i = 0; st[i]; i++)
{
if(st[i] == '(' || st[i] == '{' || st[i] == '[')
{
Q.push(st[i]);
}
else if(st[i] == ')' || st[i] == '}' || st[i] == ']')
{
if(Q.empty() || !match(Q.top(),st[i]))
{
t = 0;
break;
}
else if(match(Q.top(),st[i]))
Q.pop();
}
}
if(Q.empty() && t)
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
}
}
栈与队列:括号匹配
最新推荐文章于 2021-09-25 20:42:48 发布