//目录

括号匹配问题

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=2

#include <stdio.h>
#include <stack>
#include <map>
#include <queue>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;


string s;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        stack<char> ans;///模拟进出站
        int i;
        cin>>s;
        for(i=0; i<s.size(); i++)
        {
            if(s[i]=='['||s[i]=='(')
                ans.push(s[i]);
            else if(ans.size())
            {
                if(s[i]==')'&&ans.top()=='('||s[i]==']'&&ans.top()=='[')
                {
                    ans.pop();
                }
                else
                {
                    break;
                }
            }
            else break;
        }
        if(i==s.size()&&ans.empty())
            printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

 

posted @ 2016-03-25 21:33  小草的大树梦  阅读(306)  评论(0编辑  收藏  举报