题解 | Problem E

Problem E

https://www.nowcoder.com/practice/3bad4a646b5b47b9b85e3dcb9488a8c3

#include <iostream>
#include <stack>
#include <string>
#include <unordered_map>
using namespace std;
unordered_map<char,char>search={{')','('},{']','['},{'}','{'}};
int main() {
    string str;
    int n;
    cin>>n;
    while (cin >> str) { // 注意 while 处理多个 case
        stack<char> st;
        bool istrue = true;
        for(int i=0;i<str.size();i++){
            if(str[i]=='('||str[i]=='['||str[i]=='{'){
                st.push(str[i]);
            }
            if(str[i]==')'||str[i]==']'||str[i]=='}'){
                if(st.empty()){
                    istrue=false;
                    break;
                }
                char tmp = st.top();
                st.pop();
                if(tmp==search[str[i]])continue;
                else {
                    istrue=false;
                    break;
                }
            }
        }
        if(!st.empty())istrue=false;
        if(istrue)puts("yes");
        else puts("no");
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

昨天 20:34
门头沟学院 Java
点赞 评论 收藏
分享
03-26 13:44
南华大学 Java
在看面经的花生米很野蛮:这种情况下你当然要回答,你也是吗!!!!我超喜欢他的XXXXX
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务