题解 | 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")

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 14:10
啊啊啊啊好幸福,妈妈是我找工作发疯前的一束光
榕城小榕树:你是我见过最幸福的牛客男孩
点赞 评论 收藏
分享
07-09 19:25
门头沟学院 Java
这是要把每一个投校招的都开盒吗?
26届之耻将大局逆转:裁人的时候一次性追回餐费
点赞 评论 收藏
分享
程序员小白条:找的太晚,别人都是大三实习,然后大四秋招春招的,你大四下了才去实习,晚1年
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务