The problem of pairing parentheses--the experience of solving problems

Source: Internet
Author: User
Tags compact

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.

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 Input3([]) (([()])))([()[]()])() Sample Output
Yesnoyes

Problem Solving Ideas:

Attention:

1. The matching bracket sequence length must be even

2. The first parenthesis must be an opening parenthesis (' (' or ' [')

3. Parentheses do not cross-nest (for example: ([)] do not conform), with symmetry.

4, a line if the "\ n" string also output "Yes".

If we currently see an opening parenthesis, we cannot tell which bracket it matches, because the parentheses that match it must be behind it, so we also need to search backwards. But when we see a closing parenthesis, we can certainly determine whether it matches the previous brace (or no match). So our first match starts with the first closing parenthesis, and when we find a pair of matching parentheses, we can remove the parentheses and continue to find the next closing parenthesis until the algorithm ends. Obviously the first closing parenthesis we meet must match the previous parenthesis, otherwise the third rule is not met. When we delete the first matching parentheses, the remaining parentheses form a new sequence. You can then apply the above ideas to continue the line matching.

Feature: The following parentheses are removed first, and the front parenthesis is finally deleted. This rule conforms to the last-in-first-out rule of the stack.

Program code:

#include <iostream>#include<cstdio>#include<stack>#include<cstring>using namespacestd; Charstr[ $]; BOOLJudgeCharXChary) {if(x=='['&&y==']')return 1; if(x=='('&&y==')')return 1; return 0; }  BOOLLeftCharx) {if(x=='['|| x=='(')return 1; return 0; }  intMain () {intT;cin>>T;      GetChar ();  while(t--) {Stack<Char>s;           Gets (str); if(strcmp (str,"\ n")==0) {cout<<"Yes"<<endl;Continue;}  for(intI=0; I<strlen (str); i++)          {             if(S.empty ())//determine if the stack is emptyS.push (Str[i]); Else                if(!judge (S.top (), Str[i])) {                 if(Left (Str[i])) S.push (Str[i]); }             ElseS.pop (); }          if(S.empty ()) cout<<"Yes"<<Endl; Elsecout<<"No"<<Endl; }      return 0; }

The problem of pairing parentheses--the experience of solving problems

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.