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

全部评论

相关推荐

大飞的诡术妖姬:之前看b站多明海有个说法,日本就业竞争非常低的原因不光是毕业学生少,还有很多人干两年不喜欢职场氛围就辞职躺平,位置也空了很多,论吃苦耐劳还得看咱们
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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