acm--Bracket Matching C + + implementation __c++

Source: Internet
Author: User
Description now, there is a line of parentheses, please check that the brackets are paired. Enter the first line to enter a number n (0<n<=100) to indicate that there are N groups of test data. The next n rows are entered into multiple sets of input data, each set of input data is a string s (S is less than 10000, and S is not an empty string), and the number of test data sets is less than 5 groups. Data guarantee S contains only "[", "]", "(", ")" Four characters output

The output of each set of input data is one row, and if the parentheses contained in the string are paired, the output yes, and no if not paired


Sample input

3
[(])
(])
([[] ()])
Sample output
No
no
Yes

#include <stdio.h>
#include <string.h>

int main ()
{
    int n,i,top;
    scanf ("%d", &n);
    GetChar ();
    while (n--)
    {
        char s[10001],ch;
        Top=-1;
        while ((Ch=getchar ())!= ' \ n ')
        {
            if (ch== ') ' &&top>=0&&s[top]== ' (')     //top>=0  ±íã÷õ»²»îª¿õ
                top--;
            else
                if (ch== '] ' &&top>=0&&s[top]== ')
                    top--;
            else
                s[++top]=ch;
        }
        if (top==-1)
            printf ("yes\n");
        else
            printf ("no\n");
    }
    return 0;
}

Bracket matching this problem in Nanyang OJ's official network, we can write their own code and then go to the above to submit a test, URL: Click to open the link






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.