POJ 1363(栈)

来源:互联网 发布:萨迪克哈佐维克数据 编辑:程序博客网 时间:2024/06/05 07:32
#include <iostream>#include "stdio.h"#include "stdlib.h"#include "string.h"#include "algorithm"#include <queue>#include <stack>#define N 100005using namespace std;int main(){    int n;    int target[1005];    stack<int> s;    while(scanf("%d", &n) != EOF && n)    {        while(scanf("%d", &target[1]) != EOF)        {            if(target[1] == 0) break;            while(!s.empty()) s.pop();            for(int i=2; i <= n; i++)                scanf("%d", &target[i]);                        int a = 1, b = 1, leap = 1;            while(b <= n){                if( !s.empty() && s.top() == target[b] ) { b++; s.pop(); } //如果栈不为空且栈顶元素等于目标的元素                 else if( a <= n ) { s.push(a++); } //如果不满足第一个条件且 火车未全部进栈                 else { leap = 0; break; }            }            if( leap ) printf("Yes\n");            else printf("No\n");        }        printf("\n");    }    return 0;}

0 0