brackets! brackets!

Source: Internet
Author: User

brackets! brackets!
Time limit:1000 MS Memory limit:65536 K
Total submit:698 (239 users) Total accepted:272 (users) Rating: Special Judge:no
Description

There is six kinds of brackets: ' (', ') ', ' [', '] ', ' {', '} '. DCCMX ' s girl friend is now learning Java programming language, and got mad with brackets! Now give you a string of brackets. Is it valid? For example: "(([{{}]))" is valid, but "([)]" was not.

Input

First line contains a integer T (t<=10): The number of the test case.

Next T lines, each contains a string:the input expression consists of brackets.

The length of a string is between 1 and 100.

Output

For each test case, the output "Valid" in one line if the expression is Valid, or "Invalid" if not.

Sample Input
2
{{[[(())]]}}
({[}])
Sample Output
Valid
InvalidNote () () This is a valid case. () () () This situation will make flag==0; () (This situation will cause the stack to be empty.) If a closing parenthesis appears, then if the stack is not empty, the top element of the stack and the right parenthesis match. Then the two conditions can not be satisfied under the conditions of flag==0;
#include <iostream> #include <string.h> #include <stack>using namespace Std;int main () {int T;char gq[ 101];cin>>t;while (t--) {stack<char>ls;scanf ("%s", &gq); int flag=1;for (int i=0;i<strlen (GQ); i++) {if (gq[i]== ' | | gq[i]== ' {' | | | gq[i]== ' [') Ls.push (Gq[i]), else    {if (gq[i]== ') ') {if (!ls.empty () &&ls.top () = = ' (') {Ls.pop ();} Else{flag=0;break;}} else if (gq[i]== '} ') {if (!ls.empty () &&ls.top () = = ' {') {Ls.pop ();} Else{flag=0;break;}} else if (gq[i]== '] ') {if (!ls.empty () &&ls.top () = = ' [') {Ls.pop ();} else {flag=0;break;}}}  if (!ls.empty () | | | flag==0) cout<< "Invalid" <<endl;elsecout<< "Valid" <<ENDL;} return 0;}

brackets! brackets!

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.