UVA 637 Parentheses Balance(栈)

Posted 韵祈

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA 637 Parentheses Balance(栈)相关的知识,希望对你有一定的参考价值。

题目代号:HDU 1237

题目链接: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;
}

  

以上是关于UVA 637 Parentheses Balance(栈)的主要内容,如果未能解决你的问题,请参考以下文章

UVa673 Parentheses Balance (栈)

习题 6-1 UVA-673Parentheses Balance

B - Parentheses Balance (UVA - 673)

习题6_1 平衡的括号(Parentheses Balance, UVa 673)

习题6_1 平衡的括号(Parentheses Balance, UVa 673)

AD637使用笔记