您的位置:首页 > 其它

POJ 1363(栈)

2015-11-16 22:36 399 查看
#include <iostream>
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "algorithm"
#include <queue>
#include <stack>
#define N 100005
using 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: