您的位置:首页 > 其它

括号匹配

2016-05-04 11:08 295 查看
#include<iostream>
#include<stack>
#include<cstring>
#include<cstdio>
using namespace std;
int isPass(string s)
{
stack<char>p;
for(int i=0;s[i];i++)
{
if(s[i]=='[')
{
p.push(']');
}
if(s[i]=='(')
{
p.push(')');
}
if(s[i]==')'||s[i]==']')
{
if(p.empty()) return 0;
if(s[i]!=p.top()) return 0;
p.pop();
}
}
if(!p.empty()) return 0;
else return 1;
}
int main()
{
int k;
char a[10010];
scanf("%d",&k);
getchar();
while(k--)
{
scanf("%s",a);
if(isPass(a)==1) printf("Yes\n");
else printf("No\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string