zoukankan      html  css  js  c++  java
  • UVA-673 Parentheses Balance(栈)

    题目大意:给一个括号串,看是否匹配。

    题目分析:一开始用区间DP写的,超时了。。。

    注意:空串合法。

    代码如下:

    # include<iostream>
    # include<cstdio>
    # include<stack>
    # include<cstring>
    # include<algorithm>
    using namespace std;
    
    char p[130];
    stack<char>s;
    
    bool judge()
    {
        int len=strlen(p);
        if(len==0)
            return true;
    
        while(!s.empty())
            s.pop();
        for(int i=0;i<len;++i){
            if(p[i]=='('||p[i]=='[')
                s.push(p[i]);
            else{
                if(s.empty())
                    return false;
                char c=s.top();
                if(c=='('&&p[i]!=')')
                    return false;
                if(c=='['&&p[i]!=']')
                    return false;
                s.pop();
            }
        }
        return s.empty();
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        getchar();
        while(T--)
        {
            gets(p);
            if(judge())
                printf("Yes
    ");
            else
                printf("No
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    软件工程课程设计团队项目总结与项目报告
    个人总结
    团队项目UI
    黄金点
    wordcount
    小学运算
    第七周
    第八周
    第六周博客
    第五周博客
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/4858249.html
Copyright © 2011-2022 走看看