uva 673

题意:括号配对问题  , 天啊 ,用栈被坑了好久  。。
#include <iostream>
#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;

bool judge(char a,char b)
{
    if (a == '[' && b == ']')
        return 1;
    if (a == '(' && b == ')')
        return 1;
    return 0;
}

bool left(char a)
{
    if ( a == '[' || a == '(')
        return 1;
    return 0;
}

int main()
{
    int cas;
    char s[200];
    cin>>cas;
    getchar();
    while (cas--)
    {
        stack<char>q;
        gets(s);
        if (strcmp(s,"\n") == 0)
        {
            cout<<"Yes"<<endl;
            continue;
        }

        for (int i = 0 ;s[i] ; i++)
        {
            if ( q.empty() )
                q.push(s[i]);
            else if ( !judge(q.top(),s[i]) )
            {
                if ( left(s[i]) )
                    q.push(s[i]);
            }
            else q.pop();
        }

        if (q.empty())
            cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}


你可能感兴趣的:(uva 673)