wa,0pts
查看原帖
wa,0pts
384214
esquigybcu楼主2021/4/5 20:04
#include <stdio.h>
#include <string.h>

const int SIZE = 128 + 5;
struct stack
{
    int a[SIZE], top;

    stack()
    {
        top = 0;
    }

    bool push(int n)
    {
        if (top == SIZE)
            return false;
        a[top++] = n;
        return true;
    }

    bool empty()
    {
        return top == 0;
    }

    int pop()
    {
        if (empty())
            return 0x7fffffff;
        return a[--top];
    }

    void clear()
    {
        top = 0;
    }
};

int main()
{
    int n, len;
    char str[SIZE];
    stack st;

    scanf("%d", &n);
    while (n--)
    {
        scanf("%s", str);
        len = strlen(str);
        /*
        #ifndef ONLINE_JUDGE
        printf("str = %s, len = %d\n", str, len);
        #endif
        */

        bool ok = true;
        for (int i = 0; i < len; i++)
        {
            if (str[i] == '(')
                st.push(0);
            else if (str[i] == ')')
            {
                if (st.empty() || st.pop() != 0)
                {
                    /*
                    #ifndef ONLINE_JUDGE
                    printf("str = %s fails at i = %d where st.pop() = %d\n", str, i, st.a[st.top]);
                    #endif
                    */
                    ok = false;
                    break;
                }
            }
            else if (str[i] == '[')
                st.push(1);
            else
            {
                if (st.empty() || st.pop() != 1)
                {
                    /*
                    #ifndef ONLINE_JUDGE
                    printf("str = %s fails at i = %d where st.pop() = %d\n", str, i, st.a[st.top]);
                    #endif
                    */
                    ok = false;
                    break;
                }
            }
            /*
            #ifndef ONLINE_JUDGE
            printf("on i = %d stacks looks like ", i);
            for (int j = 0; j < st.top; j++)
                printf("%d ", st.a[j]);
            printf("\n");
            printf("st.empty() is %d\n", (int)st.empty());
            #endif
            */
        }
        if (!st.empty())
            ok = false;
        if (ok)
            printf("Yes\n");
        else
            printf("No\n");
        st.clear();
    }
    return 0;
}

udebug跑了几个样例,全都是对的

可能是输入输出的问题?

2021/4/5 20:04
加载中...