uva673 Parentheses Balance

来源:互联网 发布:天涯十大悬案 知乎 编辑:程序博客网 时间:2024/05/18 01:51

#include <stdio.h>
#include <string.h>

#define LOCAL

char str[130];
char stack[130];

int main()
{
    int i, j, n;
    int len;
    int top;

    #ifndef LOCAL
        freopen("c://uva_in.txt", "r", stdin);
    #endif

    scanf("%d", &n);
    getchar();

    for (i = 0; i < n; i++)
    {
        gets(str);

        len = strlen(str);
        if (len == 0)
        {
            printf("Yes/n");
            continue;
        }

        stack[0] = str[0];
        top = 0;

        for (j = 1; j < len; j++)
        {
            if (str[j] == '(' || str[j] == '[')
                stack[++top] = str[j];
            else if (str[j] == ')')
            {
                if (top >= 0 && stack[top] == '(')
                    --top;
                else
                    stack[++top] = str[j];
            } else
            {
                if (top >= 0 && stack[top] == '[')
                    --top;
                else
                    stack[++top] == str[j];

            }
        }

        if (top == -1)
            printf("Yes/n");
        else
            printf("No/n");


    }


    return 0;
}

原创粉丝点击