题目

POJ-1363 Rails
Description

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, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track.

The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has N <= 1000 coaches numbered in increasing order 1, 2, …, N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1, a2, …, aN. Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station.

Input

The input consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of 1, 2, …, N. The last line of the block contains just 0.

The last block consists of just one line containing 0.
Output

The output contains the lines corresponding to the lines with permutations in the input. A line of the output contains Yes if it is possible to marshal the coaches in the order required on the corresponding line of the input. Otherwise it contains No. In addition, there is one empty line after the lines corresponding to one block of the input. There is no line in the output corresponding to the last ``null’’ block of the input.
Sample Input

5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0
Sample Output

Yes
No

Yes

题目翻译

有数字序列1,2,…n这样严格顺序入栈。由于进栈出栈时间不同,所以有很多合法的组合。只有符合栈的规则的,即后进先出的才是合法的。

下面举例说明不合法:1,2,3按照顺序入栈,如果出现1,2这样的出栈即是不合法。因为1,2 不满足后进先出的规则,应该是2在前1再后出栈。

解题思路

采用队列queue和栈stack组合使用。

  1. 队列queue保存待判断的序列,比如样例中的5 4 1 2 3
  2. 1,2,,,n按照顺序进栈stack,判断栈顶元素top()是否等于队首元素front(),如果相等,则说明有合法的可能性,同时弹出队首元素和栈顶元素
  3. 最后如果栈空,则说明待判断的序列是合法的序列。

代码说明:
这里的函数参数为队列,需要传进来一个队列。看似多此一举,实则锻炼自己形参形式的转化能力。
是不是还没用过队列queue作为函数的形参呢?

代码

#include<iostream>
#include<queue>
#include<stack>using namespace std;bool check(queue<int> &order)
{stack<int> S;int n=order.size();//长度for(int i=0;i<n;++i){S.push(i+1);//入栈while(!S.empty()&&order.front()==S.top())//队首==栈顶{order.pop();//同时弹出S.pop();}} if(!S.empty())return false;return true;  //栈为空,说明合法
}int main()
{queue<int> Q;int T,temp1,temp,a[10000];while(1){cin>>T;//读入位数if(T==0) break;while(1){cin>>a[0];//读入一行数据if(a[0]==0) break; //第一个数据为零,则退出到上层whileQ.push(a[0]);for(int i=1;i<T;++i){cin>>a[i];//借助数组,进入队列Q.push(a[i]);}if(check(Q))//调用判断合法函数cout<<"Yes"<<endl;elsecout<<"No"<<endl;while(!Q.empty())//清空队列Q.pop();}cout<<endl;//输出空行,跟位数T是一组。}return 0;
}

总结:
该题考查基本的栈的应用。
该题需要注意输入输出的格式。

5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0

这样的输入该怎么处理呢?

可以看出这里结束录入的条件是输入0 回车 0 回车。即:

0
0

同时还要求是同一位数n的数据输出结束后,输出一个空行。

while(1)
{cin>>n;//位数if(n==0) break;//退出该层循环//下面读入每一行数据,这时候要考虑开头为0,则为结束的标志。while(1){cin>>a[0];if(a[0]==0) break;for(int i=1;i<n;++i)cin<<a[i];}
cout<<endl;//这里和位数n并列
}

希望本次总结对你有帮助。

POJ1363Rails队列和栈应用相关推荐

  1. leetcode-225 队列实现栈

    使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() – 获取栈顶元素 empty() – 返回栈是否为空 队列的特点:先入先出 栈的特点:后入先出 ...

  2. 【C++】STL队列和栈的使用

    C++的STL标准模板库提供了队列和栈的基本操作.下面通过两个demo分别介绍STL队列和STL栈的使用. Demo1:STL队列 [题目]卡片游戏(题目来自刘汝佳<算法竞赛入门>) 桌上 ...

  3. LeetCode 225. Implement Stack using Queues--用队列实现栈--C++解法

    LeetCode 225. Implement Stack using Queues–用队列实现栈–C++解法 LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在 ...

  4. python中的队列和栈_python的队列和栈

    (一)队列和栈的区别 1.队列: 队列是一种特殊的线性表.其两头都有限制,插入只能在表的一端进行(只进不出),而删除只能在表的另一端进行(只出不进),允许删除的一端称为队尾(rear),允许插入的一端 ...

  5. Leetcode刷题 225题:用队列实现栈(基于Java和c++两种语言)

    ** Leetcode刷题 225题:用队列实现栈(基于Java和c++两种语言) ** 题目: 使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top( ...

  6. python【力扣LeetCode算法题库】225-用队列实现栈

    用队列实现栈 使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() – 获取栈顶元素 empty() – 返回栈是否为空 注意: 你只能使用队列的基 ...

  7. 【C++ 语言】容器 ( queue 队列 | stack 栈 | priority_queue 优先级队列 | set 集合 | 容器遍历 | map )

    文章目录 queue 队列 stack 栈 priority_queue 优先级队列 priority_queue 优先级队列指定排序方法 priority_queue 优先级队列排序行为 prior ...

  8. 数据结构与算法:链表,队列,栈,递归,有序表

    反转单链表,双链表 import java.util.ArrayList; import java.util.List;public class ReverseList {public static ...

  9. 用栈实现队列和用队列实现栈

    首先需要使用上篇文章(用数组实现栈和队列)中的栈和队列两个类 1.栈实现队列:思路是有两个栈,一个用来放数据(数据栈),一个用来辅助(辅助栈).数据添加时,会依次压人栈,取数据时肯定会取栈顶元素,但我 ...

最新文章

  1. Linux(64) 下 Tomcat + java 环境搭建
  2. python下划线变量的含义
  3. php将字符串转成json字符串数组,php数组怎么转成json字符串
  4. 径向涡轮膨胀机行业调研报告 - 市场现状分析与发展前景预测
  5. CAD2004软件从下载安装到学习CAD教程(后台菜单自助更多)
  6. 超级搜索术3-吸收应用/一键直达
  7. BlackBerry 9850 应用:BBM, Windows Live Messenger (msn)
  8. python3 读文件 编码_Pyhton3下的ISO8859-1编码文件的读取
  9. 移动端开发旅游预约_套餐列表页面动态展示_套餐详情页面动态展示
  10. 面试智力题,1000瓶水,其中一瓶有毒,而且毒性无敌,稀释一亿倍毒性都不减,毒性的发作时间最长为1小时,请问怎样可以在两个小时之内找出哪瓶水有毒
  11. 为什么社会上的Java程序员还没有饱和?
  12. php快捷方式 图标ie,pubwin删除IE快捷方式并自行创建IE快捷方式的问题解决方法...
  13. 多视几何009:对极几何
  14. FATAL: Atom .R<CYM 383>.A<H 11> does not have a type.
  15. 使用eclipse或者myeclipse时,鼠标变成黑色十字架解决办法
  16. 韩服游戏IP用哪里的比较稳定怎么选择服务器
  17. window下Nexus私服高级搭建
  18. 有效运用 Color mask 和开发 Automation material - PART 1
  19. android待机动画,Android 忆童年 DVD机待机 loading 动画
  20. 互联网人的乐理基础(三)

热门文章

  1. Redis总结(二)C#中如何使用redis
  2. Leetcode OJ: Remove Duplicates from Sorted Array I/II
  3. Web应用界面设计规范
  4. 获取表中的某个字段名
  5. centos 安装vscode_CentOS6下安装VSCode
  6. 0.0 目录-深度学习第五课《序列模型》-Stanford吴恩达教授
  7. 1.3 更多边缘检测内容-深度学习第四课《卷积神经网络》-Stanford吴恩达教授
  8. 如何理解ScanDef的概念
  9. ubuntu18.04安装VCS+verdi错误集锦
  10. Linux下GBK文件编码批量转换UTF-8命令