acm-Bracket Pairing Problem __ACM

Source: Internet
Author: User

Description:
Now, there is a row of parentheses, please check that the brackets are paired.
Enter:
The first line enters a number of n (0

Import Java.util.Scanner;
Import Java.util.Stack;
        public class Demo01 {public static void main (string[] args) {Scanner sc = new Scanner (system.in);
        int num = Sc.nextint ();
        string[] str = new String[num];
        for (int i = 0;i < num;i++) {Str[i] = Sc.next (); for (int i = 0;i<num;i++) {if (Matchinfo (str[i)) = = True) {System.out.println ("Yes
            ");
            }else{System.out.println ("NO"); }} public static Boolean matchinfo (String str) {stack<character> Stack = new Stack<c
        Haracter> ();
        char[] CA = Str.tochararray (); if ((Character) ca[0]== ' [') | | ((Character) ca[0]== ' ('))
                {for (int index=0;index<ca.length;index++) {Character C1 = (Character) Ca[index]; if (c1== ' [') | | (c1== ' ('))
                {Stack.push (C1);
}else if (Stack.empty () ==true) {//test stack is empty                    return false; }else if ((c1== ']) | | (c1== ')) {//peek () view the object at the top of the stack, but does not remove it if (((Character) stack.peek ()) = = ' (') && Amp (C1 = =)) | | ((((Character) stack.peek ()) = = ' [') && (c1== ']))
                        {Stack.pop ()//Removes the value from the top of the stack and returns the object as the value of this function} else{
                    return false;
        }}///test stack is empty and returns true if and only if no items are included in the stack, return false to Stack.empty ();
    else return false;
 } 


}

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.