NYOJ 2 括号配对问题

来源:互联网 发布:mybatis 源码 编辑:程序博客网 时间:2024/06/01 23:37

括号配对问题
时间限制:3000 ms | 内存限制:65535 KB
难度:3
描述
现在,有一行括号序列,请你检查这行括号是否配对。
输入
第一行输入一个数N(0

3[(])(])([[]()])

样例输出

NoNoYes

以下附上代码:

#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<stack>#include<math.h>using namespace std;int main(){    int t;    scanf("%d",&t);    while(t--)    {        char a[10002];        stack<char>s;        scanf("%s",a);        int i;        int len=strlen(a);        for(i=0; i<len; i++)        {            if(s.empty())            {                s.push(a[i]);            }            else            {                if(s.top()+1==a[i]||s.top()+2==a[i])                {                    s.pop();                }                else                {                    s.push(a[i]);                }            }        }        if(s.empty())            printf("Yes\n");        else            printf("No\n");    }    return 0;}
0 0