您的位置:首页 > 其它

UVA 673(括号配对)

2015-07-31 20:51 288 查看
题目[点击即可]

解题思路 :在栈为空的情况下,判断括号的左右,然后一步一步的判断;

#include<iostream>
#include<stack>
using namespace std;
const int maxn=130;
int main()
{
int n;
cin>>n;
cin.get();
while(n--)
{
int k=1;
stack<char>a;
char ch;
while(cin.get(ch)&&ch!='\n')
{
if(ch==')'&&k)
{
if(a.empty())
k=0;
else if(a.top()='(')
a.pop();
else
k=0;

}
else if(ch==']'&&k)
{
if(a.empty())
k=0;
else  if(a.top()=='[')
a.pop();
else
k=0;

}
else
a.push(ch);

}
if(a.empty()&&k)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: