括号配对问题

来源:互联网 发布:painter是什么软件 编辑:程序博客网 时间:2024/05/18 09:05

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

#include<iostream>#include<stdio.h>#include<string.h>#include<stack>using namespace std;char ch[10055];char sh[10055];int main(){    int T;    scanf("%d",&T);    while(T--)    {        //stack<char> Q;        scanf("%s",ch);        int len=strlen(ch);        getchar();        if(len%2!=0)        {            printf("No\n");            continue;        }        int top=-1;        sh[++top]=ch[0];        for(int i=1;i<len;i++)        {            if(ch[i]=='('||ch[i]=='[')            {                sh[++top]=ch[i];            }             // Q.push(ch[i]);            if(ch[i]==')')            {                if('('==sh[top])                    top--;                else                    sh[++top]=ch[i];            }            else if(ch[i]==']')            {                if('['==sh[top])                    top--;                else                    sh[++top]=ch[i];            }        }        if(top<0)            printf("Yes\n");        else            printf("No\n");    }    return 0;}
1 0
原创粉丝点击