【POJ】[1363]Rails

这里写图片描述

思路和上面的那一题
【杭电】[1022]Train Problem I
差不多

这个只需要判断能否出栈
而不用输出过程
所以省略了flag数组等一些无关的变量

#include<stdio.h>
#include<stack>
using namespace std;
int main() {
    int n;
    bool flag=false;
    while(scanf("%d",&n),n) {
        if(flag)
            printf("
");
        int a[1200];
        while(scanf("%d",&a[1]),a[1]) {
            stack<int>s;
            for(int i=2; i<=n; i++)
                scanf("%d",&a[i]);
            int j=1;
            for(int i=1; i<=n; i++) {
                s.push(i);
                while(!s.empty()&&s.top()==a[j]) {
                    s.pop();
                    j++;
                }
            }
            if(j-1==n)
                printf("Yes
");
            else
                printf("No
");
        }
        flag=true;
    }
    return 0;
}

题目地址:【POJ】[1363]Rails

原文地址:https://www.cnblogs.com/BoilTask/p/12569715.html