Rails UVA-514 (stack)

vjudge链接

原题链接

  • 题目大意

判断列车是否能以特定顺序离开车站,车厢最多1000辆。

  • 分析

可以将铁轨重叠部分看作一个栈。车厢陆续入栈,当栈顶的车厢号和需要出栈的车厢号相同时将该车厢出站。若所有车厢都已经进入却依然无法满足要求时,可以判断无法以该顺序离开车站。

/*
 *lang C++ 5.3.0
 *user Weilin_C
*/

#include <iostream>
#include <stack>

using namespace std;

int main()
{
    stack <int> ista;
    int n;
    int no[1005];

    while (scanf("%d", &n) && n) {
        while (scanf("%d", &no[0]) && no[0]) {
            for (int i=1; i<n; i++) scanf("%d", &no[i]);
            while (!ista.empty()) ista.pop();
            int outn=0, car=1;
            while (outn!=n && car<=n+1) {
                if (ista.empty() || ista.top()!=no[outn]) ista.push(car++);
                else {ista.pop(); outn++;}
            }
            if (outn==n) printf("Yes\n");
            else printf("No\n");
        }
        putchar('\n');
    }

    return 0;
}

by SDUST weilinfox
原文链接:https://www.cnblogs.com/weilinfox/p/12273783.html

猜你喜欢

转载自www.cnblogs.com/weilinfox/p/12273783.html