HDU 1022.Train Problem I【栈的应用】【8月19】
Train Problem I
railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train
B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the
trains can get out in an order O2.



Input.
the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.
3 123 321 3 123 312
Yes. in in in out out out FINISH No. FINISH Hint Hint For the first Sample Input, we let train 1 get in, then train 2 and train 3. So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1. In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3. Now we can let train 3 leave. But after that we can't let train 1 leave before train 2, because train 2 is at the top of the railway at the moment. So we output "No.".
栈的应用:给定1个出栈序列,看当前入栈序列是否能通过入栈出栈达到这个出栈序列.代码例如以下:
- #include<cstdio>
- #include<stack>
- using namespace std;
- int main(){
- int n;
- char f1[1010],f2[1010];//f1记录入栈顺序,f2记录出栈顺序
- while(scanf("%d%s%s",&n,f1,f2)!=EOF){
- int g[1010],i=0,j=0,k=0;//g[]记录进出情况
- stack<char> t;
- while(i<n){
- if(t.empty()||t.top()!=f2[i]&&j<n){
- t.push(f1[j++]);
- g[k++]=1;//1为in
- }
- else if(t.top()==f2[i]){
- t.pop();
- g[k++]=0;//0为out
- i++;
- }
- else break;
- }
- if(t.empty()){//栈t为空,则能够生成当前出栈序列
- printf("Yes.\n");
- for(int i=0;i<k;i++){
- if(g[i]) printf("in\n");
- else printf("out\n");
- }
- printf("FINISH\n");
- }
- else printf("No.\nFINISH\n");
- }
- return 0;
- }
HDU 1022.Train Problem I【栈的应用】【8月19】的更多相关文章
- Hdu 1022 Train Problem I 栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1022 Train Problem I(栈的操作规则)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Ot ...
- hdu 1022 Train Problem I【模拟出入栈】
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU - 1022 Train Problem I STL 压栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1022 Train Problem I(栈的应用+STL)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1022:Train Problem I(数据结构,栈,递归,dfs)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1022 Train Problem I[给出两个长n的串,入栈和出栈顺序,判断入栈顺序是否可以匹配出栈顺序]
Train Problem I 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 As the new term comes, the Ignatius Train Sta ...
- HDU 1022 Train Problem I
A - Train Problem I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- hdu 1022 Train Problem
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- 有了这些,java IO就不愁了
IO的总结: java中相对路径和绝对路径的问题: 在web项目中,如果生成的文件前面没有 / 开头的话,表示的是生成的文件在当前项目的根目录下如student.txt在项目中刷新就能看到. 如果是以 ...
- ESP8266基础篇
ESP8266基础篇 模块刚到如图所示~2016-01-26 后三张是手机APP的截图,前面是两个ESP8266的硬件模块,当然大家不应定要买两个,自己焊锡一个USB转TTL的串口就行了,有了下面的底 ...
- XX-NET史上最详细完整教程
转 https://www.cnblogs.com/phperkang/p/8780123.html 前言 XX-NET,系GAE类代理,即通过可用Google ip连接Google App Eng ...
- POJ 2001 Shortest Prefixes 【Trie树】
<题目链接> 题目大意: 找出能唯一标示一个字符串的最短前缀,如果找不出,就输出该字符串. 解题分析: Trie树的简单应用,对于每个单词的插入,都在相应字符对应的节点 num 值+1 , ...
- HDU 4614 Vases and Flowers 【线段树】+【二分】
<题目链接> 题目大意: 有n个花瓶,每个花瓶中只能放一朵花.两种操作,一种是从A开始放F朵花,如果有的花瓶中已经有花则跳过这个花瓶,往下一个花瓶放:第二种是将区间[A,B]之间花瓶中的花 ...
- HDU1232——畅通工程【并查集】
<题目链接> 题目大意: 利用并查集找出图中有几个不连通的城镇集合,所需修的道路数即为城镇集合-1. #include <stdio.h> ]; int find(int x) ...
- puppeteer 安装失败的解决方案
你应该也是在玩puppeteer,所以才会来到这里寻找答案,开始正文…… 安装时遇到 ERROR: Failed to download Chromium r515411! Set "PUP ...
- sql语句1——join
注:一下内容来自于http://www.w3school.com.cn/sql/ 1. 引用两个表 我们可以通过引用两个表的方式,从两个表中获取数据: 谁订购了产品,并且他们订购了什么产品? sele ...
- BZOJ.4821.[SDOI2017]相关分析(线段树)
BZOJ LOJ 洛谷 恶心的拆式子..然后就是要维护\(\sum x_i,\ \sum y_i,\ \sum x_iy_i,\ \sum x_i^2\). 操作三可以看成初始化一遍,然后同操作二. ...
- 机器学习系列-tensorflow-03-线性回归Linear Regression
利用tensorflow实现数据的线性回归 导入相关库 import tensorflow as tf import numpy import matplotlib.pyplot as plt rng ...