题干:

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.
FINISHFor 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.".

Hint

Hint

解题报告:

刚开始天真的以为直接翻转数组,如果匹配就YES否则就NO。但是一想这题没这么简单,因为比如:我一共要停4辆车,我可以先停三辆,然后出两辆,然后再进一辆,这样也是符合题意的。

别忘了这里用了数据结构,所以任何操作都必须判断一步栈是否为空。

AC代码:

#include<bits/stdc++.h>using namespace std;
int opr[1005];
int main()
{char in[15],out[15];int n;while(~scanf("%d",&n)) {int top = 0;scanf("%s%s",in,out);stack<char > sk;int cur=0;for(int i = 0; i<n; i++) {sk.push(in[i]);opr[++top] = 1;if(sk.size()) {while(sk.size() && sk.top() == out[cur]) {cur++;sk.pop();opr[++top] = 0;}} }if(sk.empty()) {puts("Yes.");for(int i = 1; i<=top; i++) {opr[i] == 1 ? puts("in") : puts("out");}}else puts("No.");puts("FINISH");}return 0 ;
}

【HDU - 1022】Train Problem I (栈模拟,水题,思维)相关推荐

  1. HDU 1022[Train Problem I] 栈的应用

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 题目大意:一列序列为s1的车厢是否能通过车站的中转以序列s2出站.白皮上有. 关键思想:栈的应用 ...

  2. hdu 1022 Train Problem I 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 其实是一道上数据结构课讲过的例题  只不过当时讲的是怎么用手算  一道水题又花了好长时间 其实就 ...

  3. hdu 1022 Train Problem I(栈)

    标记现在已经匹配到o1的第几列车了,o2从头开始,如果不匹配把o1就放入栈中,匹配后出栈并比较栈顶与o1的下一辆,匹配继续出,不匹配就继续进栈 1 #include <iostream> ...

  4. HDU 1022 Train Problem I stack 基础题

    题意: 有一个火车站,只有一个口同时用于进入和出去,但不能有2辆或以上同时进出. 给出一个n代表有n辆火车(n<=9),编号分别为1到n. 然后给出2个序列,分别代表火车进站的顺序,火车出站的顺 ...

  5. 【HDU 1889】Reaux! Sham! Beaux!(模拟+水题)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1889 题意:给出一个表,每个国家的石头剪子布的叫法,给两个人用自己国家的语言进行石头剪子布,进行统计,最 ...

  6. train problem I (栈水题)

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

  7. 【HDOJ】1022 Train Problem I_天涯浪子_新浪博客

    [题目]http://acm.hdu.edu.cn/showproblem.php?pid=1022 [报告] 模拟,直接模拟一个栈的运行就行了. [程序] // Task: 1022 Train P ...

  8. 【CCCC】L2-027 名人堂与代金券 (25分),模拟水题

    problem L2-027 名人堂与代金券 (25分) 对于在中国大学MOOC(http://www.icourse163.org/ )学习"数据结构"课程的学生,想要获得一张合 ...

  9. 洛谷 1563 玩具谜题——模拟水题

    题目:https://www.luogu.org/problemnew/show/P1563 模拟水题. #include<iostream> #include<cstdio> ...

  10. hdu 1023 Train Problem II

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

最新文章

  1. PCA(2):PCA算法实现的两种方式
  2. linux chattr命令
  3. 为什么经常遇到无效证书?
  4. EVC实现拷贝文件夹下所有文件
  5. 122亿元消费券引爆首都初夏,京东618如何成为经济复苏加速引擎
  6. javascript之构造器
  7. 二 关键词---关键词的选择(二)
  8. 29 个 PHP 的 Excel 处理类
  9. mysql集群搭建.pdf,内容太过真实
  10. java怎么写自定义布局_java-Android设置自定义首选项布局
  11. PHP数组的访问方法有几种,PHP数组的几种遍历方法
  12. 修改MAC地址的方法(未测试)
  13. 「ubuntu」在Ubuntu Server 16.04 LTS下安装VMware Tools(转)
  14. pic单片机c语言 程序,单片机教程:PIC单片机C语言程序设计(一)
  15. 基于springboot+mysql的房地产中介管理系统
  16. OBS教程:3分钟学会直播推流与视频录制
  17. GMP编译make check时出现FAIL t-scan
  18. Portapack应用开发教程(四)GPS应用具体更改
  19. 腾讯云重装和还原操作系统
  20. 左右连接和内连接的区别

热门文章

  1. 计算机科学中的研究成果,田聪教授团队科研成果在计算机科学顶会LICS 2020发表...
  2. thincmf 分页无法搜索_thinkcmf5 后台搜索分页保持分页条件
  3. mysql galera 下载_Mysql-Galera Cluster
  4. A. Red and Blue Beans
  5. 天正计算机命令大全,天正CAD 中按Ctrl+v在不同图中粘贴出现“未知命令T81_tpasteclip”,直接在CAD中就能操作...
  6. python做的游戏可以导出吗_Python for RenderDoc批量导出模型和贴图
  7. ef mysql 外键 一对一_EFCore-一对一配置外键小记2
  8. V4L2应用程序框架
  9. xxl-job 执行结果是空_xxljob dotnet core executor执行器开源
  10. linux 修改Db2主机名,修改DB2服务器的主机名