zoukankan      html  css  js  c++  java
  • nyist 2 括号配对问题

    括号配对问题

    时间限制:3000 ms  |  内存限制:65535 KB
    难度:3
     
    描述
    现在,有一行括号序列,请你检查这行括号是否配对。
     
    输入
    第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符
    输出
    每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No
    样例输入
    3
    [(])
    (])
    ([[]()])
    样例输出
    No
    No
    Yes
    #include <iostream>
    #include <stack>
    using namespace std;
    int main()
    {
        int N;
        cin>>N;
        while(N--)
        {
            stack<char> s;
            char st[10005];
            int f=1,i=0;
            cin>>st;
            for(;st[i]&&f;i++)
                if(st[i]=='('||st[i]=='[')s.push(st[i]);
                else
                switch(st[i])
                {
                    case ')':
                        if(!s.empty()&&s.top()=='(')s.pop();
                        else f=0;
                        break;
                    case ']':
                        if(!s.empty()&&s.top()=='[')s.pop();
                        else f=0;
                        break;
                }
            if(f&&s.empty())cout<<"Yes
    ";
            else cout<<"No
    ";
        }
        return 0;
    }
    View Code

    #include <iostream>
    #include <stack>
    using namespace std;
    int main()
    {
     int N;
     cin>>N;
     while(N--)
     {
      stack<char> s;
      char st[10005];
      int f=1,i=0;
      cin>>st;
      for(;st[i]&&f;i++)
       if(st[i]=='('||st[i]=='[')s.push(st[i]);
       else
       switch(st[i])
       {
        case ')':
         if(!s.empty()&&s.top()=='(')s.pop();
         else f=0;
         break;
        case ']':
         if(!s.empty()&&s.top()=='[')s.pop();
         else f=0;
         break;
       }
      if(f&&s.empty())cout<<"Yes ";
      else cout<<"No ";
     }
     return 0;
    }

     
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <stack>
    using namespace std;
    
    int main()
    {
        int n,i,bz;
        char s[10008];
        stack <char> my;
    
        cin>>n;
        gets(s);
        while (n--)
        {
            gets(s);
            for (i=0;i<strlen(s);i++)
            {
                if ( s[i]== ')')
                    if ( !my.empty() && my.top()=='(' )
                    {
                        my.pop();
                        continue;
                    }
                if ( s[i]== ']' )
                    if ( !my.empty() && my.top()=='[' )
                    {
                        my.pop();
                        continue;
                    }
                my.push(s[i]);
            }
            if ( my.empty() ) cout<<"Yes
    ";
            else cout<<"No
    ";
            while (!my.empty())   my.pop();
        }
        return 0;
    }
            
    View Code

      #include <iostream>

    #include <cstring>

    #include <cstdio>

    #include <stack>

    using namespace std;

    int main()

    {  

       int n,i,bz;  

       char s[10008];  

       stack <char> my;

        cin>>n;

        gets(s);  

       while (n--)  

       {     

        gets(s);

            for (i=0;i<strlen(s);i++)

            {  

               if ( s[i]== ')')  

                   if ( !my.empty() && my.top()=='(' )   

                  {                     my.pop();              continue;                 }      

           if ( s[i]== ']' )

                    if ( !my.empty() && my.top()=='[' )

                    {                     my.pop();                     continue;                 }

                my.push(s[i]);

            }   

          if ( my.empty() )                    cout<<"Yes ";

            else cout<<"No ";

            while (!my.empty())   my.pop();  

       }   

      return 0;

    }        

  • 相关阅读:
    可闭环、可沉淀、可持续的企业级数据赋能体系
    案例解读|迁云的灵魂3问,降多少本,增多少效,真平滑否?
    Serverless 实战——使用 Rendertron 搭建 Headless Chrome 渲染解决方案
    从零开始入门 K8s | etcd 性能优化实践
    State Processor API:如何读取,写入和修改 Flink 应用程序的状态
    阿里云叔同:以容器为代表的云原生技术,已成为释放云价值的最短路径
    Flink SQL 如何实现数据流的 Join?
    仅1年GitHub Star数翻倍,Flink 做了什么?
    codeforces div2_603 F. Economic Difficulties(树dfs预处理+dp)
    codeforces div2_604 E. Beautiful Mirrors(期望+费马小定理)
  • 原文地址:https://www.cnblogs.com/2014acm/p/3887121.html
Copyright © 2011-2022 走看看