Bracket matching problem stack implementation

Source: Internet
Author: User

[Html] <SPAN style = "FONT-SIZE: 14px"> parentheses matching problem time limit: 3000 MS | memory limit: 65535 KB difficulty: 3 Description now, there is a sequence of parentheses, check whether this line of parentheses is paired. Enter N (0 <N <= 100) In the first line, indicating that N groups of test data exist. The next N rows Input Multiple groups of input data. Each group of input data is a string of S (the length of S is less than 10000, and S is not an empty string), and the number of test data groups is less than five. Data guarantee S only contains "[", "]", "(", ")" and outputs each group of input data to occupy one row, if the parentheses contained in the string are paired, Yes is output. If not paired, No is output. Input 3 [(]) ([[] ()]) sample output No Yes </SPAN> bracket pairing problem time limit: 3000 MS | memory limit: 65535 KB difficulty: 3 Description now, there is a sequence of parentheses, check whether this line of parentheses is paired. Enter N (0 <N <= 100) In the first line, indicating that N groups of test data exist. The next N rows Input Multiple groups of input data. Each group of input data is a string of S (the length of S is less than 10000, and S is not an empty string), and the number of test data groups is less than five. Data guarantee S only contains "[", "]", "(", ")" and outputs each group of input data to occupy one row, if the parentheses contained in the string are paired, Yes is output. If not paired, No is output. Input 3 [(]) ([[] ()]) sample output NoNoYes [cpp] <SPAN style = "FONT-SIZE: 14px" >#include <cstdio >#include <stack >#include <cstring> using namespace std; int main () {int test; // freopen ("D: // ACMInput/input.txt", "r", stdin); scanf ("% d", & test ); while (test --) {stack <char> number; char c [10050]; scanf ("% s", c); int flag = 0; if (strlen (c) % 2 = 1) {Printf ("No \ n"); continue;} while (! Number. empty () {number. pop () ;}for (int I = 0; I <strlen (c); I ++) {if (c [I] = '[' | c [I] = '(') {number. push (c [I]);} else if (c [I] = ']') {if (! Number. empty () {char c = number. top (); number. pop (); if (c = '[') {continue;} else flag = 1;} else flag = 1;} else if (c [I] = ') ') {if (! Number. empty () {char c = number. top (); number. pop (); if (c = '(') {continue;} else flag = 1;} else flag = 1;} if (flag = 1) break ;} if (flag = 1) printf ("No \ n"); else printf ("Yes \ n") ;}return 0 ;} </SPAN> # include <cstdio> # include <stack> # include <cstring> using namespace std; int main () {int test; // freopen ("D: // ACMInput/input.txt "," r ", stdin); scanf (" % d ", & test); while (test --) {stack <char> number; char c [1 0050]; scanf ("% s", c); int flag = 0; if (strlen (c) % 2 = 1) {printf ("No \ n "); continue;} while (! Number. empty () {number. pop () ;}for (int I = 0; I <strlen (c); I ++) {if (c [I] = '[' | c [I] = '(') {number. push (c [I]);} else if (c [I] = ']') {if (! Number. empty () {char c = number. top (); number. pop (); if (c = '[') {continue;} else flag = 1;} else flag = 1;} else if (c [I] = ') ') {if (! Number. empty () {char c = number. top (); number. pop (); if (c = '(') {continue;} else flag = 1;} else flag = 1;} if (flag = 1) break ;} if (flag = 1) printf ("No \ n"); else printf ("Yes \ n") ;}return 0 ;}

Related Article

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.