U199256 括号 求助

题目总版

luozezhong @ 2024-11-03 14:56:27

U199256 括号 code:

#include<iostream>
#include<stack>
using namespace std;
bool foo(){
    stack<bool>my_stack;
    string s;
    cin>>s;
    for(auto it=s.begin();it!=s.end();it++){
        if(*it=='('){
            my_stack.push(false);
        }else if(*it==')'){
            if(my_stack.empty()){
                return 0;
            }else{
                my_stack.pop();
            }
        }
    }
    return 1;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    for(int i=1;i<=3;i++){
        cout<<(foo()?"Yes":"No")<<'\n';
    }
    return 0;
}

玄关


by syp11 @ 2024-11-03 14:59:52

有可能是左括号过多,最后要判断栈是否为空。

#include<iostream>
#include<stack>
using namespace std;
bool foo(){
    stack<bool>my_stack;
    string s;
    cin>>s;
    for(auto it=s.begin();it!=s.end();it++){
        if(*it=='('){
            my_stack.push(false);
        }else if(*it==')'){
            if(my_stack.empty()){
                return 0;
            }else{
                my_stack.pop();
            }
        }
    }
    return my_stack.empty();
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    for(int i=1;i<=3;i++){
        cout<<(foo()?"Yes":"No")<<'\n';
    }
    return 0;
}

by syp11 @ 2024-11-03 15:00:04

@luozezhong


by chaihaozhe101 @ 2024-11-03 15:02:39

666


by luozezhong @ 2024-11-03 15:16:09

@syp11 抱歉没看见,谢谢,此贴结


|