Uva514——Rails

来源:互联网 发布:sql应用程序在哪里 编辑:程序博客网 时间:2024/04/29 19:44

考栈的简单应用。书本的答案有点小错误。

#include<cstdio>#include<stack>using namespace std;const int MANX = 1010;int n, c[MANX];int main(){    while (scanf("%d",&n)==1)    {        while (scanf("%d", &c[1])==1)        {            if (!c[1])            {                printf("\n");                break;            }        for (int i = 2; i <= n; i++)        {            scanf("%d", &c[i]);        }        int a = 1, b = 1, ok = 1;        stack<int> s;        while (b<=n)        {            if (a == c[b]) { a++; b++; }            else if (!s.empty() && s.top() == c[b]) { s.pop(); b++; }            else if (a <= n) { s.push(a++); }            else            {                ok = 0; break;            }        }        printf("%s\n", ok ? "Yes" : "No");        }    }    return 0;}

原创粉丝点击