#include <iostream>
#include <string>
using namespace std;
typedef struct
{
char data[10000];
int top;
}stack;
void compare(stack *old,char tmp)
{
if((old->data[old->top]=='('&&tmp==')')||(old->data[old->top]=='['&&tmp==']'))
{
old->data[old->top]=NULL;
old->top--;
}
else
{
old->top++;
old->data[old->top]=tmp;
}
}
void initstack(stack *st)
{
st->top=-1;
}
int main()
{
string test;
stack endstring;
initstack(&endstring);
int num;
cin>>num;
while(num--)
{
cin>>test;
for(int i=0;i<test.size();i++)
{
if(i==0)
{
endstring.top++;
endstring.data[endstring.top]=test[0];
continue;
}
else
{
compare(&endstring,test[i]);
}
}
// for(int j=0;j<endstring.top+1;j++)
// cout<<endstring.data[j];
if(endstring.top==-1)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
initstack(&endstring);
}
return 0;
}
括号配对问题
最新推荐文章于 2018-08-29 18:42:00 发布
本文介绍了一个使用栈数据结构实现的括号匹配算法。该算法能够判断字符串中的括号是否正确配对,适用于编程语言解析器或编辑器的语法检查功能。通过输入一系列包含括号的字符串,程序会返回括号是否匹配的结果。

7513

被折叠的 条评论
为什么被折叠?



