您的位置:首页 > 理论基础 > 数据结构算法

2134-数据结构实验之栈与队列四:括号匹配

2017-10-19 19:46 190 查看
#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;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: