UVA 514 - Rails && POJ 1363 (堆栈模拟)

给一个出栈顺序,问其是否可以由1,2,3,4,5. ....... N通过进栈得到,只会用堆栈模拟。。。。
/*===============================================================
*   Copyright (C) 2012 All rights reserved.
*   
*   file: uva514.cpp
*   author: ivapple
*   date: 2012-09-19
*   description: 
*
*   update log: 
*
================================================================*/
#include <cstdlib>
#include <cstdio>
//#include <stack>

#include <iostream>

#define out(x) (cout<<#x<<": "<<x<<endl)

#define FOR(i,s,t) for(i=s; i<t; i++)

using namespace std;

template<class T>void show(T a, int n){int i; for(i=0;i<n;i++)cout<<a[i]<<" ";cout<<endl;}

template<class T>void show(T a, int r, int l){int i; for(i=0;i<r;i++)show(a[i],l);cout<<endl;}

const int kMaxNum = 1000;

int input[kMaxNum+5];
int N;
int stack[kMaxNum+2];
int front = 0;
int top = 0;

bool Check()
{
	int i = 0;
	int st;
	front = 1;
	top = 0;
	while(1)
	{
		while (!top || stack[top-1] < input[i]) stack[top++] = front++;
		st = stack[--top];
		if (st > input[i]) 
			return 0;
		if (st == input[i]) 
			i++;
		if (i == N) return 1;
	}
}

int main()
{
	int num;
	int i;
#ifndef ONLINE_JUDGE
	freopen("test.txt", "r", stdin);
#endif
	while (scanf("%d", &N)&&N)
	{
		while(scanf("%d", &num)&&num)
		{
			input[0] = num;
			for (i=1; i<N; i++)
				scanf("%d", &input[i]);
			if (Check())
				printf("Yes\n");
			else
				printf("No\n");
		}
		printf("\n");
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值