样例能过,但全WA
查看原帖
样例能过,但全WA
750558
_Hu_Tao楼主2022/9/3 14:52

求助!代码如下:

#include <bits/stdc++.h>
using namespace std;
char s[200];
int main() {
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>s;
        int x=strlen(s);
        //for(int i=0;i<x;i++){
        //    cout<<s[i]<<" ";
        //}
        stack<char>stk;
        while(!stk.empty())stk.pop();
        int f=1;
        if(x==0){
            cout<<"Yes\n";
            continue;
        }
        for(int j=0;j<x;j++){
            if(s[j]=='('){
                stk.push('(');
            }else if(s[j]=='[')
            {
                stk.push('[');
            }else if(s[j]==')'){
                if(stk.empty()){
                    f=0;
                }
                else{
                    if(stk.top()=='('){
                        stk.pop();
                    }else{
                        f=0;
                    }
                }
            }else if(s[j]==']'){
                if(stk.empty()){
                    f=0;
                }else{
                    if(stk.top()=='['){
                        stk.pop();
                    }else{
                        f=0;
                    }
                }
            }
        }
        if(f==1&&stk.empty()){
            cout<<"Yes\n";
        }else cout<<"No\n";
    }
    return 0;
}

2022/9/3 14:52
加载中...