给出一串字符串,里面只包含“[","(",")","]",判断字符串里面的括号是否正确配对,
#include<iostream>
#include<algorithm>
using namespace std;
char st1[1000];
int main()
{
int a,b,c,t,n,m;
char ch,ch1,ch2;
cin>>a;
getchar();
while(a--)
{
t=0;
while((ch=getchar())!='\n')//设定输入单个字符的输入方式为循环输入
{
if(ch==')'&& st1[t-1]=='(')
t--;
else if(ch==']'&& st1[t-1]=='[')
t--; //把“(”,“[”,从栈中弹出,
else
st1[t++]=ch;//把“(”,“[”,放入栈;
}
if(t==0)//如果最终栈是空的,说明括号配对是正确的
cout<<"yes";
else
cout<<"no";
cout<<endl;
}
}
括号配对问题(用数组来模拟栈的思想)
最新推荐文章于 2020-06-07 17:12:19 发布