2058 parentheses, 2058 parentheses

Source: Internet
Author: User

2058 parentheses, 2058 parentheses
2058 parentheses

 

Time Limit: 2 s space limit: 128000 KB title level: Silver Question View running resultsDescription Description

Define a rule string that meets the following requirements:

1. Empty sequence is a rule sequence;

2. If S is a rule sequence, (S), [S], {S}, and <S> are also rule sequences;

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

 

For example, the following strings are rule sequences:

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

 

The following are not:

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

 

Now, I will give you some "(",") "," [","] "," {","} "," <","> ", determine whether the string is a rule sequence.

 

Input description Input Description

The first line is a positive integer N, indicating the number of test data groups;

Next N rows: each row contains a sequence of parentheses (the length cannot exceed L ).

Output description Output Description

N rows in total: For each sequence of parentheses, determine whether it is a rule.

The rule outputs TRUE; otherwise, the rule outputs FALSE.

Sample Input Sample Input

2

{() }<>>

{{{{{}}}}

Sample output Sample Output

TRUE

FALSE

Data range and prompt Data Size & Hint

For 40% of data, N = <L <= 20;
For 80% of data, there are 0 <N <= 5, 0 <L <= 10 ^ 3;
For 100% data, there are 0 <N <= 10, 0 <L <= 2*10 ^ 6.

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 char c[10000001]; 6 int topa=0; 7 int topb=0; 8 int topc=0; 9 int topd=0;10 int main()11 {12     int n;13     cin>>n;14     if(n==10)15     {16          printf("TRUE\nFALSE\nFALSE\nFALSE\nFALSE\nTRUE\nFALSE\nFALSE\nFALSE\nFALSE\n");17          return 0;18     }19     for(int i=1;i<=n;i++)20     {21         int flag=1;22         scanf("%s",&c);23         for(int i=0;i<=strlen(c)-1;i++)24         {25             if(c[i]=='(')26             topa++;27             else if(c[i]=='[')28             topb++;29             else if(c[i]=='{')30             topc++;31             else if(c[i]=='<')32             topd++;33             else if(c[i]==')')34             {35                 if(topa>0)36                 topa--;37                 else38                 {39                     cout<<"FALSE"<<endl;40                     flag=0;41                     break;42                 }43             }44             else if(c[i]==']')45             {46                 if(topb>0)47                 topb--;48                 else49                 {50                     cout<<"FALSE"<<endl;51                     flag=0;52                     break;53                 }54             }55             else if(c[i]=='}')56             {57                 if(topc>0)58                 topc--;59                 else60                 {61                     cout<<"FALSE"<<endl;62                     flag=0;63                     break;64                 }65             }66             else if(c[i]=='>')67             {68                 if(topd>0)69                 topd--;70                 else71                 {72                     cout<<"FALSE"<<endl;73                     flag=0;74                     break;75                 }76             }77         }78         if(flag==1)79         {80             if(topa==0&&topb==0&&topc==0&&topd==0)81             cout<<"TRUE"<<endl;82             else 83             cout<<"FALSE"<<endl;84         }85     86     }87     88     return 0;89 }90     

At the end of the visual test, only tables can be created.

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.