题解 | #Problem E#

Problem E

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

#include <iostream>
#include <stack>
using namespace std;

int main() {
    int a;
    int flag = 1;
    string expression;
    stack<char> st;
    while (cin >> a) { // 注意 while 处理多个 case
        while (a--) {
            cin >> expression;
            for (int i = 0; i < expression.length(); i++) {
                if (expression[i] == '[' || expression[i] == '(' || expression[i] == '{')
                    st.push(expression[i]);
                if (!st.empty() && expression[i] == ']' && st.top()=='[')
                    st.pop();
                else if (!st.empty() && expression[i] == ')' && st.top()=='(')
                    st.pop();
                else if (!st.empty() && expression[i] == '}' && st.top()=='{')
                    st.pop();
                else if (expression[i] == ')' || expression[i] == ']' || expression[i] == '}'){
                    cout<<"no"<<endl;
                    flag = 0;
                    break;
                }
                    
            }
            if(st.empty() && flag!=0)
                cout<<"yes"<<endl;
            else if(flag){
                cout<<"no"<<endl;
            }
            while(!st.empty())
                    st.pop();
            flag = 1;
        }
        
    }

}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

Java抽象带篮子:简历怎么写可以看看我发的帖子,你的第一个是实习经历吗?那怎么写的是你的第一个练手项目呢?简历写的怎么样直接投小厂面试一下就知道了
没有实习经历,还有机会进...
点赞 评论 收藏
分享
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-03 17:30
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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