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)