栈的使用

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;
}
(•̀ᴗ•́)و
全部评论

相关推荐

牛客5655:其他公司的面试(事)吗
点赞 评论 收藏
分享
3 收藏 评论
分享
牛客网
牛客企业服务