Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23574    Accepted Submission(s): 8907

Problem Description
As
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
The
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
The
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.
 
Sample Input
3 123 321
3 123 312
 
Sample Output
Yes.
in
in
in
out
out
out
FINISH
No.
#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

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题纪念)的更多相关文章

  1. train problem I (栈水题)

    杭电1002http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/ ...

  2. Train Problem II HDU 1023 卡特兰数

    Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want ...

  3. Train Problem I hdu 1022(栈)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1022 题意:给出火车的进站与出站顺序,判断是否可以按照给出的出站顺序出站. #include < ...

  4. BZOJ 100题纪念

  5. hdu 1023 Train Problem II

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Train Problem II Description As we all know the ...

  6. HDU 1023 Train Problem II (大数卡特兰数)

    Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. HDU 1022 Train Problem I(栈的操作规则)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Ot ...

  8. HDU 1022 Train Problem I

    A - Train Problem I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  9. HDU 5832 A water problem(某水题)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

随机推荐

  1. $key 的用法

    <?php $attr=array("a","b","c","d"); //$key,默认是主键值,$value, ...

  2. firefox查看微信公众平台的数据分析时就出现不信任链接怎么办?

    昨天用360清理垃圾后火狐主页的快速拨号栏消失了,整了半天还是无法使用就重装了一下firefox,导入备份的书签,添加自己所需的附加组件,设置为隐私模式,开始继续体验.按惯例打开微信公众平台,查看数据 ...

  3. hping3命令

    hping3命令 网络测试 hping是用于生成和解析TCPIP协议数据包的开源工具.创作者是Salvatore Sanfilippo.目前最新版是hping3,支持使用tcl脚本自动化地调用其API ...

  4. scrapy和selenium结合抓取动态网页

    1.安装python (我用的是2.7版本的) 2.安装scrapy:   详情请参考 http://blog.csdn.net/wukaibo1986/article/details/8167590 ...

  5. JQgrid for asp.net

     转载自http://blog.csdn.net/shiworkyue/article/details/8283716 JQgrid for asp.net 网上资料较少,自己总结了些不全,能用到的可 ...

  6. poj 2709

    http://poj.org/problem?id=2709 题意:就是那个老师需要n瓶颜色的墨水,和1瓶颜色的灰色的墨水,但是灰色的墨水没得卖,只能由三种颜色相同的墨水混合而成,但是3瓶50ML的墨 ...

  7. windows添加和删除服务

    删除系统服务,记得一定要小心用.避免删错sc delete 服务名 加入服务: sc create 服务名 binPath= 路径 start= auto

  8. -fomit-frame-pointer 编译选项在gcc 4.8.2版本中的汇编代码研究

    #include void fun(void) { printf("fun"); } int main(int argc, char *argv[]){ fun(); return ...

  9. codeforces 479C Exams 解题报告

    题目链接:http://codeforces.com/problemset/problem/479/C 题目意思:简单来说,就是有个人需要通过 n 门考试,每场考试他可以选择ai, bi 这其中一个时 ...

  10. 有关GPU硬件的理解

    1 显卡的DRAM相当于CPU的RAM (Random access memory, 内存). 二者共同的特点是通电的时候才能使用,不正常断电数据就丢失,但正常情况下,会将数据存储到硬盘中.显存又称帧 ...