帮忙看一上 如何输出不对额。
帮忙看一下 怎么输出不对额。。
#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool Test(char a,char s)//测试两字符是否匹配()或{}或【】
{
if(a=='{'&&s=='}')
return true;
else if(a=='('&&s==')')
return true;
else if(a=='['&&s==']')
return true;
return 0;
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;++i)
{
stack<char> b;//定义栈
string s;
cin>>s;
int l=s.size();
char c;
for(int j=0;j<l;++j)
{
c=s[j];
if(!b.empty()&&Test(b.top(),c))//匹配则出栈 否则入栈
b.pop();
else
b.push(c);
}
if(b.empty())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
system("pause");
return 0;
}
------解决方案--------------------
#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool Test(char a,char s)//测试两字符是否匹配()或{}或【】
{
if(a=='{'&&s=='}')
return true;
else if(a=='('&&s==')')
return true;
else if(a=='['&&s==']')
return true;
return 0;
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;++i)
{
stack<char> b;//定义栈
string s;
cin>>s;
int l=s.size();
char c;
for(int j=0;j<l;++j)
{
c=s[j];
if(!b.empty()&&Test(b.top(),c))//匹配则出栈 否则入栈
b.pop();
else
b.push(c);
}
if(b.empty())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
system("pause");
return 0;
}
------解决方案--------------------