南阳理工2解题报告(括号配对问题)

来源:互联网 发布:人工智能与信贷的结合 编辑:程序博客网 时间:2024/05/21 02:53

简单的过了头,因为没有空串

顺便说一下,自己写栈的话会快一点点,不过没必要

time 24ms memory 240

#include<cstdio>#include<cstring>#include<stack>using namespace std;const int maxn=10000+100;char info[maxn];int main(){    int T;    scanf("%d",&T);    while(T--)    {        stack<char> s;        scanf("%s",info);        for(int i=0;i<strlen(info);i++)        {            if(info[i]==')' || info[i]==']')            {                if(s.empty())                {                    s.push(info[i]);                    break;                }                char c=s.top();                if(info[i]==')' && c=='(') s.pop();                else if(info[i]==']' && c=='[') s.pop();                else                {                    s.push(info[i]);                    break;                }            }            else s.push(info[i]);        }        if(s.empty()) printf("Yes\n");        else printf("No\n");    }    return 0;}


原创粉丝点击