例题6-2(Rails, UVa 514)

题目链接

给定入栈顺序,问能否以给出的顺序出栈。

众所周知,栈的特点是先入后出。

此特点在该题中体现为对于当前需要出栈的元素(要想先出),必须位于栈顶或者还未入栈(必须后入)。

用数组target来存储出栈序列,target[B_now] 表示当前需要驶入B的车厢(即当前需要出栈的元素),A_first表示当前A中第一个车厢(即下一个入栈元素)

 1 #include <iostream>
 2 #include <stack>
 3 using namespace std;
 4
 5 const int MAX_N = 1000 + 10;
 6
 7 int main() {
 8     int N, target[MAX_N];
 9
10     while(cin >> N && N != 0) {//多个block的循环
11         while(true) {//多个序列的循环
12             cin >> target[0];
13             if(target[0] == 0) break;
14             for(int i=1; i<N; i++)  cin >> target[i];
15             stack<int> s;
16             int A_first = 1, B_now = 0;  //设A中第一个元素为A_first,当前驶入B的元素为B_now
17             for(; B_now<N; B_now++) {
18                 while( (s.empty() || s.top() != target[B_now]) && A_first <= N) {/*如果栈空或栈顶元素不为target[B_now]
19                 且A中还有元素,从A中入栈 */
20                     s.push(A_first); A_first++;
21                 }
22                 if(s.top() == target[B_now]) s.pop();
23                 else { cout << "No" << endl; break; }
24             }
25             if(B_now == N) cout << "Yes" << endl;
26         }
27         cout << endl;
28     }
29     return 0;
30 }

My Code

下面给出书上给出的代码(注:书上的代码并不适应oj上的输入模式,故直接提交结果为WA)

 1 // UVa514 Rails
 2 // Rujia Liu
 3 #include<cstdio>
 4 #include<stack>
 5 using namespace std;
 6 const int MAXN = 1000 + 10;
 7
 8 int n, target[MAXN];
 9
10 int main() {
11   while(scanf("%d", &n) == 1) {
12     stack<int> s;
13     int A = 1, B = 1;
14     for(int i = 1; i <= n; i++)
15       scanf("%d", &target[i]);
16     int ok = 1;
17     while(B <= n) {
18       if(A == target[B]){ A++; B++; }
19       else if(!s.empty() && s.top() == target[B]){ s.pop(); B++; }
20       else if(A <= n) s.push(A++);
21       else { ok = 0; break; }
22     }
23     printf("%s\n", ok ? "Yes" : "No");
24   }
25   return 0;
26 }

原文地址:https://www.cnblogs.com/c138Rick/p/8372749.html

时间: 2024-08-01 00:35:05

例题6-2(Rails, UVa 514)的相关文章

Rails,uva 514

题目:铁轨 题目链接:UVa514链接 题目描述: 某城市有一个火车站,有n节车厢从A方向驶入车站,按进站的顺序编号为1-n.你的任务是判断是否能让它们按照某种特定的顺序进入B方向的铁轨并驶入车站.例如,出栈顺序(5 4 1 2 3)是不可能的,但是(5 4 3 2 1)是可能的. 题目分析: 为了重组车厢,借助中转站,对于每个车厢,一旦从A移入C就不能回到A了,一旦从C移入B,就不能回到C了,意思就是A->C和C->B.而且在中转站C中,车厢符合后进先出的原则.故这里可以看做为一个栈. 题目

UVa 514 Rails(经典栈)

 Rails  There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, i

[2016-02-03][UVA][514][Rails]

时间:2016-02-03 22:24:52 星期三 题目编号:UVA 514 题目大意:给定若干的火车(编号1-n),按1-n的顺序进入车站, 给出火车出站的顺序,问是否有可能存在 分析:    FIFO,用栈模拟一遍即可, 方法:    根据输入的顺序,从1-n开始,当前操作的为i 如果i是当前对应的编号,那么直接跳过(进入B) 如果不是,根据当前需求的编号,小于i,就从栈顶弹出一个元素, 看这个元素是否是需求的,是则继续.否则NO 1 2 3 4 5 6 7 8 9 10 11 12 13

UVa 514 - Rails

https://uva.onlinejudge.org/external/5/514.pdf 514 Rails There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possib

UVa 514 Rails(模拟栈)

题意  n辆火车按顺序依次进站  判断给定的出战顺序是否可能 用数组模拟模拟栈代表车站  车依次进站  每当栈顶火车序号与当前要出站的b[cur] 相等时 就让栈顶元素出栈  即top-- #include<cstdio> #include<cstring> using namespace std; const int N = 2000; int b[N], c[N]; int main() { int l, cur, top; while(scanf("%d"

UVA - 514 Rails 经典栈使用

题目大意:有一列n节车厢的火车要入栈,车厢从1到n,只能从小到大入栈. 现在给出一个出栈顺序,问能否实现 解题思路:如果栈顶元素大于要出栈的数,肯定就不能实现了. 如果栈顶元素小于要出栈的数,就继续入栈 如果栈定元素等于要出栈的数,就出栈 #include<cstdio> #include<algorithm> #include<stack> using namespace std; #define maxn 1010 int num[maxn], n; void so

UVa 514 (stack的使用) Rails

练习一下stack的使用,还有要注意一下输入的格式,看了好长时间没懂. 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <stack> 6 using namespace std; 7 8 const int maxn = 1000 + 10; 9 int n, target[maxn]; 10 11 int main(vo

UVa 514 Rails(栈的应用)

题目链接: https://cn.vjudge.net/problem/UVA-514 1 /* 2 问题 3 输入猜测出栈顺序,如果可能输出Yes,否则输出No 4 5 解题思路 6 貌似没有直接可以判定的方法,紫书上给出的方法是在进行一个入栈出栈操作,看是否能够构成原始的入栈序列. 7 */ 8 #include<cstdio> 9 #include<stack> 10 11 using namespace std; 12 int ok(int a[],int n); 13 i

UVa 514 数据结构栈

背景:1A 思路:栈模拟 我的代码: #include <set> #include <stack> #include <queue> #include <vector> #include <cstdio> #include <map> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm&g