Determines whether parentheses match in a string

Source: Internet
Author: User

Reads a string from the keyboard that contains only () {} [] to determine whether each bracket in the string appears in pairs.

Hint: can be implemented with stack, parentheses must be paired up, such as () []{}, which is a matching bracket, such as ([[{])}, which is a mismatched bracket (no spaces in the middle). Enter a description:

Enter a string (no spaces in the middle) for the output description:

Match output Yes, otherwise output no input sample:

(([{}]))
Output Sample:
Yes


Ideas:

1. Four possibilities for bracket matching: ① left and right bracket pairing sequence is incorrect
② right parenthesis more than left parenthesis
③ left parenthesis more than closing parenthesis
④ right and left bracket matching correct

2. Algorithm thought:
1. Sequential scan arithmetic expression (shown as a string), when encountered in three types of opening parenthesis, let the brackets into the stack;
2. When scanning to a certain type of closing parenthesis, compare the current stack top element to match, if match, back stack continue to judge;
3. If the current stack top element does not match the current scan bracket, then the left and right bracket pairing sequence is incorrect, the match fails, and exits directly;
4. If the string is currently a type of closing parenthesis and the stack is empty, the closing parenthesis is more than the left parenthesis, the match fails, and exits directly;
5. At the end of the string loop scan, if the stack is not empty (that is, the stack still has some type of left parenthesis), then the left parenthesis is more than the closing parenthesis, and the match fails;

6. The normal end of the brackets match correctly.



#include <iostream>
#include <cstring>
#include <stack>

using namespace std;

int main ()
{
    stack<char>a;
    int flag=1,i;
    Char ch[100];
    cin>>ch;
    for (I=0;i<strlen (CH); i++) {
        if (ch[i]== ' {') | | ch[i]== ' (' | | ch[i]== ' [')
            A.push (Ch[i]);
        else{
            if (A.empty () ==true) {
                flag=0;
                break;
            else if ((ch[i]== '} ' &&a.top () = = ' {') | | (ch[i]== ') ' &&a.top () = = ' (') | | (ch[i]== '] ' &&a.top () = = ())
                a.pop ();
            else{
                flag = 0;
                break;
            }
    }} if (flag==0)
        cout<< "No";
    else
        cout<< "yes";
}


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.