UVa 673 parentheses Balance (use of stacks)

Source: Internet
Author: User
Tags compact

StackTime 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 are 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, takes a sequence of strings of this type and check their correctness. Your program can assume, the maximum string length is 128.

InputThe file contains a positive integer Nand a sequence of NStrings of parentheses()and[], one string a line.

OutputA sequence ofYesOrNoOn the output file.

Sample Input

3 ([]) (([()]) ([[()])) ([() [] ()] () ()

Sample Output
Yesnoyes

For a beginner, the use of the stack will be confused, what is the stack? How do you use it? Many of the books are not clearly explained.

To put it simply:

Stack, also known as a stack, is a constrained linear table with restrictions that allow only one end of the table to be inserted and deleted.

The one end of the allowed operation is called the top of the stack (top), and the data element that is not allowed to operate is called the bottom of the stack (bottom), the last data element is always inserted, so the stack is also known as the "after-first-out table", which is the difference from the queue.

The storage structure of the stack has 2 kinds: a sequential storage structure (sequential stack), a chain storage structure (chain stack).

Today mainly to see how to implement a stack of functions

First, the basic functions of the stack are:

1. Empty to determine if the stack is empty

2. Pop presses a data into the stack

3. Push presses a data into the stack

4. Size returns the current stack length (that is, the number of internal data)

5. Top get stack stack tops data

This question test instructions:

Enter a sequence of parentheses containing () and [] to determine whether it is legal.  

The specific recursive definitions are as follows: 1. Null string legality, 2. If both A and B are legal, AB is legal, 3. If A is legal then (a) and [a] are legal.

Note that the input may have an empty string

Directly with the stack

#include <iostream>#include<stack>#include<cstring>#include<cstdio>using namespacestd;BOOLJudgeCharACharb) {if(a=='['&&b==']')return 1; if(a=='('&&b==')')return 1; return 0;}BOOLLeftChara) {if(a=='['|| a=='(')return 1; return 0;}intMain () {intCAs; Chars[ $]; CIN>>CAs; GetChar (); while(cas--) {Stack<Char>Q;gets (s); if(strcmp (s),"\ n")==0) {cout<<"Yes"<<Endl; Continue;}  for(intI=0; s[i];i++) {if(Q.empty ()) {Q.push (s[i]);} Else if(!judge (Q.top (), s[i])) {if(Left (S[i])) Q.push (S[i]);} ElseQ.pop ();} if(Q.empty ()) cout<<"Yes"<<Endl; Elsecout<<"No"<<Endl;} return 0;}

My own is the following one, may be easier to understand some, is also the idea of the stack

#include <stdio.h>#include<string.h>intMain () {inti,t; intFlag; ints[135],j,top; Charc[135]; scanf ("%d",&T); GetChar (); while(t--) {fgets (c,sizeof(c), stdin);//consider a blank line (c: string, sizeof (c): string length, stdin: input from keyboard)flag=0; J=0;  for(i=0; c[i]!='\ n'; i++) {if(c[i]=='(') S[j++]=0; Else if(c[i]=='[') S[j++]=1; Else if(c[i]==')') {if(j!=0&& s[j-1]==0) J--; Else{flag=1;  Break; }}Else if(c[i]==']') {if(j!=0&& s[j-1]==1) J--; Else{flag=1;  Break; }}Else{flag=1;  Break; }}if(Flag | | j!=0)//There is no left in the stackprintf"no\n"); Elseprintf ("yes\n"); }return 0; } 

Read someone else code plus i Baidu only understand the use of the stack, found to learn a lot.

UVa 673 parentheses Balance (use of stacks)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.