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.
![](http://acm.hdu.edu.cn/data/images/1022-1.jpg)
![](http://acm.hdu.edu.cn/data/images/1022-2.jpg)
![](http://acm.hdu.edu.cn/data/images/1022-3.jpg)
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 ...
随机推荐
- POJ 2418 Hardwood Species
Hardwood Species Time Limit: 10000MS Memory Limit ...
- cocos基础教程(2)Window环境下搭建(补充)
一.环境搭建 1.JDK.Eclipse与SDK 我用的JDK是1.7 Eclipse用的是Luna版的 这些之前都已经设好了,相关下载自己网上找吧 2. 下载最新的Cocos2d-x,我下的是3.5 ...
- Android相机、相册获取图片显示并保存到SD卡
Android相机.相册获取图片显示并保存到SD卡 [复制链接] 电梯直达 楼主 发表于 2013-3-13 19:51:43 | 只看该作者 |只看大图 本帖最后由 happy小妖同学 ...
- encode与decode,unicode与中文乱码的问题
encode是指将unicode字符编码成其他字符集的字符,如utf-8,ascii等: 而decode是指将其他字符编码,如utf-8转换成unicode编码. encode是指将人类用的语言(字符 ...
- 【SpringMVC】SpringMVC系列3之@PathVariable映射URL占位符参数
3.@PathVariable映射URL占位符参数 3.1.概述 带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义. ...
- Android Intent不可传递大数据
今天用intent传递一个bitmap,结果一直出错,intent无法执行,原来是intent不能传递大数据导致的,具体是多大,不太清楚,但我传递的bitmap在1m以上.
- iOS 和 Android 中的Alert
iOS 和 Android中都有alert这种提示框,下面简单介绍下. ios中的alert叫做UIAlertView,共有4种样式,由于在ios7上,自定义alertview不太好用,所以也就这4种 ...
- iOS 中的Certificate,Provisioning Profile 等在code singing中用到的信息
注册apple id 有1年多了,这些概念还是模模糊糊的,决定在这里总结一下. 请参阅官方文档 App Distribution Guide code singing的作用如下: Code signi ...
- codeforces B. Vasya and Public Transport 解题报告
题目链接:http://codeforces.com/problemset/problem/355/B 题目意思:给出四种票种,c1: 某一部bus或者trolley的单程票(暗含只可以乘坐一次):c ...
- python文件取MD5
import hashlib def md5sum(filename, blocksize=65536): hash = hashlib.md5() with open(filename, " ...