【ACM】括号配对问题

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

题目描述

现在,有一行括号序列,请你检查这行括号是否配对。

输入

第一行输入一个数N

输出

每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No

样例输入

3

[ ( ] )

( ] )

( [ [ ] ( ) ] )

样例输出

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");
    }   
}
发布了46 篇原创文章 · 获赞 39 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_42128813/article/details/103591407