码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 637 Parentheses Balance(栈)

时间:2017-07-17 15:18:48      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:map   ace   using   category   sig   option   check   nsis   contains   

题目代号: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(栈)

标签:map   ace   using   category   sig   option   check   nsis   contains   

原文地址:http://www.cnblogs.com/teble/p/7194138.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!