UVA 514 - Rails && POJ 1363 (堆栈模拟)
2012-09-19 22:10
531 查看
给一个出栈顺序,问其是否可以由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; }
相关文章推荐
- poj 1363 Rails in PopPush City &&【求堆栈中合法出栈顺序次数】
- Uva 514 Rails (STL 栈+队列 模拟)
- POJ 1363 Rails(模拟/数据结构)
- POJ 1036 Rails 模拟堆栈
- POJ 1363 (简单栈)UVA 514
- UVA 514 - Rails(模拟)
- stack&&UVa514 Rails (铁轨)的理解与解析
- UVA 514 Rails 铁轨 栈的模拟
- UVA 514 Rails(模拟|栈)
- POJ 1363 Rails(栈,模拟地铁出站)
- POJ-1363 Rails-堆栈入门
- POJ 1036 Rails 模拟堆栈
- POJ 1363 Rails(模拟栈的过程)
- Rails UVA 514(栈模拟,水题)
- poj-1363-Rails(栈)
- 铁轨(Rails,ACM/ICPC CERC 1997.UVa 514)
- UVa 10282 & POJ 2503 - Babelfish
- POJ 1363 Rails
- UVA-1388 && POJ-3154 墓地雕塑(数学)
- POJ 1001 & UVa 748 - Exponentiation in JAVA