Train Problem I (HDU 100题纪念)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23574 Accepted Submission(s): 8907
the new term comes, the Ignatius Train Station is very busy nowadays. A
lot of student want to get back to school by train(because the trains
in the Ignatius Train Station is the fastest all over the world ^v^).
But here comes a problem, there is only one 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 contains several test cases. Each test case consists of an
integer, the number of trains, and two strings, the order of the trains
come in:O1, and the order of the trains leave:O2. The input is
terminated by the end of file. More details in the Sample Input.
output contains a string "No." if you can't exchange O2 to O1, or you
should output a line contains "Yes.", and then output your way in
exchanging the order(you should output "in" for a train getting into the
railway, and "out" for a train getting out of the railway). Print a
line contains "FINISH" after each test case. More details in the Sample
Output.
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
char in[12];
char out[12];
int n;
void solve()
{
queue<char>Q;//火车出站序列
queue<int>ans;//进出顺序,1表示in,0表示out
stack<char>S;//火车入栈序列
for(int i=0; i<n; ++i)
Q.push(out[i]);
for(int i=0; i<n; i++){
if(!S.empty()&&S.top()==Q.front()){//wa了一次在这里,没有考虑到例子5 14325 13245
Q.pop();S.pop();
ans.push(0);
i--;//由于是用前一个和Q.front()比较,所以是s[i]先不入栈
}
else if(in[i]==Q.front()){
Q.pop();
ans.push(1);
ans.push(0);
}
else{
ans.push(1);
S.push(in[i]);
}
}
while(!S.empty()&&!Q.empty()){
if(S.top()==Q.front()){
S.pop();Q.pop();
ans.push(0);
}
else break;
}
while(!S.empty()){
if(S.top()==Q.front()){
ans.push(0);
S.pop();Q.pop();
}
else break;
}
if(S.empty()){
printf("Yes.\n");
while(!ans.empty()){
if(ans.front()) printf("in\n");
else printf("out\n");
ans.pop();
}
printf("FINISH\n");
}
else printf("No.\nFINISH\n");
}
int main()
{
while(~scanf("%d%s%s",&n,in,out))
solve();
return 0;
}
FINISH
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.".
Train Problem I (HDU 100题纪念)的更多相关文章
- train problem I (栈水题)
杭电1002http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/ ...
- Train Problem II HDU 1023 卡特兰数
Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want ...
- Train Problem I hdu 1022(栈)
http://acm.split.hdu.edu.cn/showproblem.php?pid=1022 题意:给出火车的进站与出站顺序,判断是否可以按照给出的出站顺序出站. #include < ...
- BZOJ 100题纪念
- hdu 1023 Train Problem II
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Train Problem II Description As we all know the ...
- HDU 1023 Train Problem II (大数卡特兰数)
Train Problem II 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
A - Train Problem I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU 5832 A water problem(某水题)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
随机推荐
- 如何让Ubuntu系统支持WebP图片格式
本文主要向大家介绍如何让 Ubuntu 系统支持查看 WebP 图片格式,以及如何将 WebP 转为 JPEG 或 PNG 图片格式的方法. 什么是WebP图片 Google开发并推出 WebP 图片 ...
- django程序报错CSRF verification failed. Request aborted.
django程序的html页面中form的method='post'的时候报错 Forbidden (403) CSRF verification failed. Request aborted.He ...
- Apache Common DbUtils
前段时间使用了Apache Common DbUtils这个工具,在此留个印,以备不时查看.大家都知道现在市面上的数据库访问层的框架很多,当然很多都是包含了OR-Mapping工作步骤的 例如大家常用 ...
- JS 自定义正则表达式
1. 正则表达式规则 1.1 普通字符 字母.数字.汉字.下划线.以及后边章节中没有特殊定义的标点符号,都是"普通字符".表达式中的普通字符,在匹配一个字符串的时候,匹配与之相同的 ...
- MySQL目录
MySQL的学习总结目录 Mysql5.7安装及配置 教你如何3分钟玩转MYSQL MySQL使用详解--根据个人学习总结 Mysql增删改 Mysql_以案例为基准之查询 MySQL之扩展(触发器, ...
- BASH相关
颜色 http://www.cnblogs.com/lr-ting/archive/2013/02/28/2936792.html http://segmentfault.com/q/10100000 ...
- ECharts2.2.0 兼容IE8
IE 8,ECharts2.2.0 版本,demo的各个功能均正常显示在IE8上面, 但是我在真正做的时候,我的html却不能显示,画面乱了,而且function也不能用, 都准备用1.4.1版本了, ...
- mysql 恢复备份
1.在my.cnf 文件中增加以下配置 log-bin=/var/lib/mysql/mysql-binloglog-bin-index = /var/lib/mysql/mysql-binlog 2 ...
- Linux服务器通过rz/sz轻松上传下载文件
Linux服务器通过命令行远程访问时,上传文件还需要ftp所以不太方便,可以使用rz这个小工具来上传不太大的文件,方法如下: 输入rz,如果提示命令不存在,证明还没有安装,以CentOS为例,安装命令 ...
- python chm 中文帮助 (2.7 和 3.4)
sphinx-build 生成的(htmlhelp) 存在2个问题 1.生成的html 编码 cp2152,需要修改/sphinx/builders/html.py ctx['encoding'] = ...