栈的使用

Problem E

http://www.nowcoder.com/questionTerminal/3bad4a646b5b47b9b85e3dcb9488a8c3

#include<stack>
#include<iostream>
#include<string>
using namespace std;
int main()
{
    int n,i,flag;
    while(cin>>n)
    {
        while(n--)
        {
            stack<char> a;
            string s;
            cin>>s;
            for(i=0,flag=0;i<s.length();i++)
            {
                if(s[i]=='['||s[i]=='('||s[i]=='{')
                    a.push(s[i]);
                else if(s[i]==']')
                {
                    if(!a.empty())
                    {
                        if(a.top()=='[')
                            a.pop();
                        else{
                            flag=1;
                            break;
                        }                        
                    }
                    else{
                        flag=1;
                        break;
                    }
                }
                else if(s[i]==')')
                {
                    if(!a.empty())
                    {
                        if(a.top()=='(')
                            a.pop();
                        else{
                            flag=1;
                            break;
                        }                        
                    }
                    else{
                        flag=1;
                        break;
                    }                    
                }
                else if(s[i]=='}')
                {
                    if(!a.empty())
                    {
                        if(a.top()=='{')
                            a.pop();
                        else{
                            flag=1;
                            break;
                        }                        
                    } 
                    else{
                        flag=1;
                        break;
                    }                    
                }
            }
            if(!a.empty()) flag=1;
            if(flag==0) printf("yes\n");
            else  printf("no\n"); 
        }
    }
    return 0;
}
(•̀ᴗ•́)و
全部评论

相关推荐

11-24 00:11
已编辑
广东工业大学 算法工程师
避雷深圳&nbsp;&nbsp;yidao,试用期&nbsp;6&nbsp;个月。好嘛,试用期还没结束,就直接告诉你尽快找下一家吧,我谢谢您嘞
牛客75408465号:笑死,直属领导和 hr 口径都没统一,各自说了一些离谱的被裁理由,你们能不能认真一点呀,哈哈哈哈哈😅😅😅
点赞 评论 收藏
分享
10-17 10:05
已编辑
北华大学 全栈开发
牛客872465272号:掉头发了哥
点赞 评论 收藏
分享
3 收藏 评论
分享
牛客网
牛客企业服务