Bracket sequence (stack)

Source: Internet
Author: User

Title Description Description

The definition satisfies the following rule string for the rule sequence, otherwise it is not a rule sequence:

1. An empty sequence is a sequence of rules;

2. If S is a sequence of rules, then (s), [S],{s} and <S> are also regular sequences;

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

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

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

And the following are not:

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

Now, give you some strings consisting of "(", ")", "[", "]", "{", "}", "<", ">", and determine if the string is a sequence of rules.

Enter a description Input Description

First line: A positive integer n, indicating the number of test data groups;

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

Output description Output Description

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

The rule outputs true, otherwise the output is false.

Sample input Sample Input

2

{()}<<>>

{{{{{}}}}

Sample output Sample Output

TRUE

FALSE

Data range and Tips 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 is 0<n<=10,0<l<=2*10^6.

#include <iostream>#include<cstring>using namespacestd;strings;intN;intMain () {CIN>>N;  while(n--) {cin>>s; Charsta[2000001],top=0; intlen=s.length (); intk=0;  while(k<Len) {sta[++top]=S[k]; if(sta[top]-sta[top-1]==2|| sta[top]-sta[top-1]==1) top-=2; K++; }        if(top==0) cout<<"TRUE"<<Endl; Elsecout<<"FALSE"<<Endl; }}

Bracket sequence (stack)

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.