Stack-summertraining#2.a problem

Source: Internet
Author: User

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.

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
Yesnoyes

The code is as follows:

1#include <iostream>2#include <stdio.h>3#include <stack>4#include <string.h>5 using namespacestd;6 7 intMain ()8 {9     intN;Ten      while(SCANF ("%d", &n)! =EOF) One     { A         Charstack[ -]; -         Charstr[ -]; - GetChar (); Because get () takes a newline character as an end, note that GetChar () absorbs line breaks.  the          while(gets (str)) -         { -              -             inti,top=-1; +              for(i=0; I<strlen (str); i++) -             { +                 if(str[i]=='('|| str[i]=='[') Astack[++top]=Str[i]; at                  -                  -                 if(str[i]==')'&& stack[top]!='(') -                 {   -top=0; -                      Break; in                 } -                 Else if(str[i]==']'&& stack[top]!='[') to                 { +top=0; -                      Break; the                 } *                  $                 if(str[i]==')'&& stack[top]=='(')Panax Notoginsengtop--; -                  the                 Else if(str[i]==']'&& stack[top]=='[') +top--; A              the                  +             } -              $             if(top==-1) $printf"yes\n"); -             Else -printf"no\n"); the         } -     }Wuyi      the     return 0; -}

Originally asked to implement the operation of the stack themselves, but not familiar with their own writing, had to use STL temporarily first over the problem. Also refer to the code of the great God, read a few days of the book Baidu Many stacks of related usage, stack of array representation and code implementation of the stack. Although I have been studying C and C + + for a year, I can feel very unfamiliar about the contest problem. Now in re-supplementing learning C and C + +. No matter what, every day is more than yesterday's self-understanding, even if progress is very little, do not give up easily. Can not see hope is very scary sad thing, not be optimistic also does not matter, own Ah, to hold the life of the rope not be left and right not to be disrupted pace.

The second week is coming to an end, well summed up. Well.

Stack-summertraining#2.a problem

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.