2058 bracket Sequence __code[vs]

Source: Internet
Author: User
Topic Description Description

The definition satisfies the following rule string as a sequence of rules, otherwise it is not a sequence of rules:

1. An empty sequence is a rule sequence;

2. If S is the rule sequence, then (s), [S],{s} and <S> is also the rule sequence;

3. If both A and B are rule sequences, then AB is also the rule sequence.

For example, the following string is a sequence of rules:

(), [], (()), ([]), () [], () [()],{{}}<>, ([]<>{{}}),<<{}>>

And the following are not:

(,[,],) (, ()), ([(), <<,{(}),<{}>)

Now, to give you a string consisting of "(", ")", "[", "]", "{", "}", "<", ">", determine if the string is a rule sequence.

Input description Input Description

First line: A positive integer n representing the number of test data sets;

Next n lines: A sequence of parentheses per line (length not exceeding L). Output Description Description

Total n rows: For each bracket sequence, determine whether it is a rule.

The rule outputs true, otherwise the output is false. Sample Input For example

2

{()}<<>>

Sample output for {{{{}}}}

TRUE

FALSE range and hint data Size & Hint

For 40% data, there are n=1,0<l<=20;
For 80% data, there are 0<n<=5,0<l<=10^3;
For 100% data, there are 0<n<=10,0<l<=2*10^6. bracket pairing problem, write with stack must be very simple, (first use the stack to do the topic) first of all, the functions in C + + using the standard library stack and queue, first contains the relevant header file


#include <stack>


#include <queue>


definition stack is as follows:


stack Stk;


The definition queues are as follows: Queue


q;




Stack provides the following stack of operations
S.empty () returns               true if the stack is null or false
s.size ()                returns the number of elements in the stack
s.pop ()                 deletes the top element of the stack but does not return its value
s.top ()                 returns the element at the top of the stack, but does not delete the element
S.push ()                presses the new element at the top of the stack

The queue provides the following actions
Q.empty () returns               true if the queue is null or false
q.size ()                returns the number of elements in the queue
q.pop ()                 deletes the first element of the queue but does not return its value
Q.front ()               returns the value of the first element of the team, but does not delete the element
Q.push ()                presses the new element at the end of the team
Q.back ()                returns the value of the tail element of the queue, but does not delete the element


#include <iostream> #include <stack> #include <string.h> #include <cstdio> using namespace std;
Stack Stk;
	int main () {int T;
	Long n,i;
	char c;
	Char map[2000001]={0};
	scanf ("%d", &t);
		while (t--) {scanf ("%s", map);
		N=strlen (map)-1; stack<char>stk;//defines a stack for (i=0;i<=n;i++) {if (map[i]==) (' | | map[i]== ' < ' | | map[i]== ' {' | |
			map[i]== ' [')//If the opening parenthesis, into the stack Stk.push (Map[i]); if (map[i]== ') ' | | map[i]== ' > ' | | map[i]== '} ' | |
					map[i]== ']/If it is a closing parenthesis, remove the top element of the stack to compare {if (Stk.empty ()) {printf ("false\n");
				Break
					C=stk.top ()///remove stack top element if (map[i]== ') ' &&c!= ' (') {printf (' false\n ');
				Break
					} if (map[i]== ' > ' &&c!= ' < ') {printf ("false\n");
				Break
					} if (map[i]== '] ' &&c!= ' [') {printf ("false\n");
				Break
					} if (map[i]== '} ' &&c!= ' {') {printf ("false\n");
				Break } stk.pop ()//pop-up stack top element}} if (I==N+1&AMP;&AMP;STK.EMpty ()) printf ("true\n");
		
	if (I==n+1&&!stk.empty ()) printf ("false\n"); }
}


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.