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.
FINISHFor the first Sample Input, we let train 1 get in, then train 2 and train 3.HintHint
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 ...
随机推荐
- ESP8266基础篇
ESP8266基础篇 模块刚到如图所示~2016-01-26 后三张是手机APP的截图,前面是两个ESP8266的硬件模块,当然大家不应定要买两个,自己焊锡一个USB转TTL的串口就行了,有了下面的底 ...
- hdu 2181 哈密顿绕行世界问题【DFS】
题目链接 题目大意: Problem Description 一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市. Input ...
- Windows下MySQL绿色版安装配置与使用
Mysql-5.7.11-winx64操作步骤: 一.安装MySQL数据库 1.下载. 下载地址: http://downloads.mysql.com/archives/get/file/mysql ...
- 课堂练习&课下作业----用户场景分析
典型用户1 ·小明---一名普通大学生生 名字 小明 性别.年龄 男,19岁 职业 大学生 收入 无 知识层次和能力 石家庄铁道大学学生,善于交际. 生活/工作情况 成绩优异,各方面技能突出 动机,目 ...
- XamarinAndroid组件教程RecylerView适配器设置动画
XamarinAndroid组件教程RecylerView适配器设置动画 本小节将讲解动画相关设置,如动画的时长.插值器以及复合动画等. 1.设置动画时长 设置动画持续的时间可以使用Animation ...
- Ubantu17.10 上安装gitlab
http://www.jianshu.com/p/92f97939e33a 亲测成功
- BZOJ.4650.[NOI2016]优秀的拆分(后缀数组 思路)
BZOJ 洛谷 令\(st[i]\)表示以\(i\)为开头有多少个\(AA\)这样的子串,\(ed[i]\)表示以\(i\)结尾有多少个\(AA\)这样的子串.那么\(Ans=\sum_{i=1}^{ ...
- ESlint开发环境配置
ESLint 是在 ECMAScript/JavaScript 代码中识别和报告模式匹配的工具,它的目标是保证代码的一致性和避免错误,是JS开发过程中极佳工具,这篇文章将以WebStorm为例告诉你如 ...
- python数据类型内置方法 字符串和列表
1.字符串 内置方法操作# a = 'qqssf'#1. print(a[-1:]) #按索引取,正向从0开始,反向从-1开始# print(len(a)) #取长度# a = 'qqssf'# 2. ...
- 基于ELK5.1(ElasticSearch, Logstash, Kibana)的一次整合
前言开源实时日志分析ELK平台(ElasticSearch, Logstash, Kibana组成),能很方便的帮我们收集日志,进行集中化的管理,并且能很方便的进行日志的统计和检索,下面基于ELK的最 ...