UVA 673 Parentheses Balance

栈的超级水题 直接模拟


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=150;
char s[MAXN], stack[MAXN];
int main()
{
    int t, i, j, top;
    scanf("%d%*c",&t);
    while(t--)
    {
        gets(s);
        top=-1;
        for(i=0; i<strlen(s); i++)
        {
            if(top>=0)
            {
                if(stack[top]=='('&&s[i]==')'||(stack[top]=='['&&s[i]==']'))
                    top--;
                else
                    stack[++top]=s[i];
            }
            else stack[++top]=s[i];
        }
        if(top==-1) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}


posted @ 2013-04-28 20:44  Ink_syk  阅读(69)  评论(0编辑  收藏  举报