[ACM] brackets matching problem

Time Limit: 3 Sec  Memory Limit: 64 MB
Submit: 669  Solved: 175

Title Description

Now, there is a line bracketed sequence, whether you check this line paired brackets.

Entry

A first input line number N

Export

Output of each input data per line, if the character string contained in parentheses are paired, output Yes, if the output is not paired No

Sample input

3

[ ( ] )

( ] )

( [ [ ] ( ) ] )

Sample Output

No

No

Yes

#include <stdio.h>
#include <string.h>
int main() {
    char s1[10001],s[10001];
    int N,a,i,j,len;
    scanf("%d",&N);
    while(N--){
        j=0;
        s1[0]=0;
        scanf("%s",s);
        len=strlen(s);
        for(i=0;i<len;i++){
            a=s[i];
            if(a==s1[j]+1|a==s1[j]+2){
               s1[j]=0;
                if(j!=0) j--;   
            }
            else {
                 if(s1[j]!=0) j++;
                 s1[j]=a;    
            }
        }
        if(s1[0]==0) printf("Yes\n");
        else printf("No\n");
    }   
}

 

Published 46 original articles · won praise 39 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_42128813/article/details/103591407