uva 673 Parentheses Balance

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

Miguel Revilla

2000-08-14

题意:
如果有一个‘(‘那么就要有一个)与它匹配,如果有一个‘[‘那么就要有一个‘]‘与它匹配.。如果都满足,输出”YES“,else 输出”NO“.
思路;
建立一个栈,左括号’[‘,‘(‘都入栈,右括号‘)‘,‘]‘都出栈依次进行下去,如果栈为空,则满足条件,输出“YES”,如果不满足条件,则输出”NO“。
代码:
#include<cstdio>
#include<stack>
using namespace std;
int main()
{
    int n;
    scanf("%d",&n);
        getchar();
        while(n--)
        {
            char a[130];
            stack<char> s;
            gets(a);
            int flag=0;
    for(int i=0;a[i]!='\0';i++)
        {
            if(a[i]=='('||a[i]=='[')
                 s.push(a[i]);
            else if(a[i]==')')
            {
                if(!s.empty()&&s.top()=='(')   s.pop();
                else
                    {flag=1;break;}
            }
            else if(a[i]==']')
            {
                if(!s.empty()&&s.top()=='[')
                    s.pop();
                else {flag=1;break;}
            }

        }
        if(flag||!s.empty())   printf("No\n");
        else printf("Yes\n");
        }
    return 0;
}

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-23 17:13:30

uva 673 Parentheses Balance的相关文章

UVA - 673 - Parentheses Balance (栈的应用!)

UVA - 673 Parentheses Balance Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Parentheses Balance  You are given a string consisting of parentheses () and []. A string of this type is said to be co

UVa 673 Parentheses Balance(括号配对 栈)

题意  判断输入的括号序列是否是配对的 栈的基础应用  栈顶元素与输入的字符匹配就出栈咯  注意括号序列可以为空 STL栈 #include <bits/stdc++.h> using namespace std; int main() { int cas; char c; cin >> cas; getchar(); while(cas--) { stack<char> s; while((c = getchar()) != '\n') { if(s.empty())

UVA 673 Parentheses Balance 解题心得

原题 Description 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 pr

UVa 673 Parentheses Balance(栈的使用)

 栈 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description 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, A

UVa 673 Parentheses Balance【栈】

题意:输入一个包含"()"和"[]"的序列,判断是否合法 用栈来模拟,遇到"(",“[”就入栈,遇到')',']'就取出栈顶元素看是否匹配,如果不匹配,则不合法 还有注意一下每次取出栈顶元素的时候判断栈是否为空,如果为空就要跳出循环 注意空串也是合法的串 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmat

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

UVA Parentheses Balance

题目如下: 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. W

UVa 673 括号平衡

思路:简单的匹配操作,利用栈. Code: #include<stdio.h> #include<string.h> char stack[135]; int main() { int n; scanf("%d",&n); getchar(); while(n-->0) { memset(stack,0,sizeof(stack)); char c; int top=0; int flag=1; while((c=getchar())!='\n')

Week 1 # E Parentheses Balance

原题描述: E - Parentheses Balance 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)