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

    题目代号:UVA 673

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=614

    题目原文:

    Parentheses Balance

    You are given a string consisting of parentheses () and []. A string of this type is said to be correct:
    (a) if it is the empty string
    (b) if A and B are correct, AB is correct,
    (c) if A is correct, (A) and [A] is correct.
    Write a program that takes a sequence of strings of this type and check their correctness. Your
    program can assume that the maximum string length is 128.


    Input
    The file contains a positive integer n and a sequence of n strings of parentheses ‘()’ and ‘[]’, one string
    a line.


    Output
    A sequence of ‘Yes’ or ‘No’ on the output file.


    Sample Input
    3
    ([])
    (([()])))
    ([()[]()])()


    Sample Output
    Yes
    No
    Yes

    题目大意:输入一个包含()和[]的括号序列,判断是否合法。1.空串合法;2.如果A和B都合法,则AB合法;3.如果A合法则(A)和[A]都合法。

    栈的练习题

    通过代码:

    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    # include <iostream>
    # include <fstream>
    # include <vector>
    # include <queue>
    # include <stack>
    # include <map>
    # include <math.h>
    # include <algorithm>
    using namespace std;
    # define pi acos(-1.0)
    # define mem(a,b) memset(a,b,sizeof(a))
    # define FOR(i,a,n) for(int i=a; i<=n; ++i)
    # define For(i,n,a) for(int i=n; i>=a; --i)
    # define FO(i,a,n) for(int i=a; i<n; ++i)
    # define Fo(i,n,a) for(int i=n; i>a ;--i)
    typedef long long LL;
    typedef unsigned long long ULL;
    
    int main()
    {
        int a,b,c,d,t;
        char ch,zhan[200];
        cin>>t;
        getchar();
        while(t--)
        {
            int top=-1;
            bool flag=true;
            while((ch=getchar())!='\n')
            {
                if(ch=='(')
                {
                    zhan[++top]='(';
                }
                else if(ch=='[')
                {
                    zhan[++top]='[';
                }
                else if(ch==')')
                {
                    if(zhan[top]=='(')
                    {
                        top--;
                    }
                    else flag=false;
                }
                else if(ch==']')
                {
                    if(zhan[top]=='[')
                    {
                        top--;
                    }
                    else flag=false;
                }
            }
            if(top>=0)flag=false;
            if(flag)cout<<"Yes"<<endl;
            else cout<<"No"<<endl;
        }
        return 0;
    }

      

  • 相关阅读:
    面向对象类成员之静态字段
    面向对象中,用super来联系父类的函数
    登录
    奇数偶数
    vue中播放音乐
    vue中的轮播图
    vue中的bind
    vue中的for
    django rest_framework中的APIView,ModelViewSet,认证,权限,频率,版本
    django rest_framework中的mixins,generics,ModelViewSet中的url简写
  • 原文地址:https://www.cnblogs.com/teble/p/7194138.html
Copyright © 2011-2022 走看看