传送门

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

  1. 3 123 321 3 123 312

Sample Output

  1. Yes.in in in out out out FINISH No. FINISH

思路

简单的栈模拟进站出站情况。
 
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<stack>
  4. using namespace std;
  5. const int maxn = 15;
  6. int main()
  7. {
  8. int N;
  9. while (~scanf("%d",&N))
  10. {
  11. bool flag = true;
  12. stack<int>stk;
  13. int i,inout[maxn] = {0},in[maxn],out[maxn];
  14. char sin[maxn],sout[maxn];
  15. scanf("%s %s",sin,sout);
  16. for (i = 0;i < N;i++) in[i] = sin[i] - '0';
  17. for (i = 0;i < N;i++) out[i] = sout[i] - '0';
  18. // for (i = 0;i < N;i++) printf("%d %d\n",in[i],out[i]);
  19. int j = 0,p = 0;
  20. i = 0;
  21. for (i = 0;i < N;i++)
  22. {
  23. if (in[i] != out[j])
  24. {
  25. inout[p++] = 0;
  26. stk.push(in[i]);
  27. }
  28. else if (in[i] == out[j])
  29. {
  30. inout[p++] = 0;
  31. j++;
  32. inout[p++] = 1;
  33. while (!stk.empty() && stk.top() == out[j])
  34. {
  35. j++;
  36. inout[p++] = 1;
  37. stk.pop();
  38. }
  39. }
  40. }
  41. while (!stk.empty())
  42. {
  43. if (stk.top() != out[j])
  44. {
  45. flag = false;
  46. break;
  47. }
  48. else
  49. {
  50. j++;
  51. stk.pop();
  52. inout[p++] = 1;
  53. }
  54. }
  55. if (!flag) printf("No.\nFINISH\n");
  56. else
  57. {
  58. printf("Yes.\n");
  59. for (i = 0;i < p;i++) inout[i]?printf("out\n"):printf("in\n");
  60. printf("FINISH\n");
  61. }
  62. }
  63. return 0;
  64. }
  65.  
  66. /*
  67. 5
  68. 12342 24321
  69. */

  

HDU 1022 Train Problem I(栈模拟)的更多相关文章

  1. hdu 1022 Train Problem I【模拟出入栈】

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

  2. Hdu 1022 Train Problem I 栈

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

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

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

  4. HDU - 1022 Train Problem I STL 压栈

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

  5. hdu 1022 Train Problem I(栈的应用+STL)

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

  6. HDU 1022.Train Problem I【栈的应用】【8月19】

    Train Problem I Problem Description As the new term comes, the Ignatius Train Station is very busy n ...

  7. hdu 1022:Train Problem I(数据结构,栈,递归,dfs)

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

  8. HDU 1022 Train Problem I[给出两个长n的串,入栈和出栈顺序,判断入栈顺序是否可以匹配出栈顺序]

    Train Problem I 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 As the new term comes, the Ignatius Train Sta ...

  9. HDU 1022 Train Problem I

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

随机推荐

  1. Oracle 数据库日常巡检

    1. 检查数据库基本状况 包含:检查Oracle实例状态,检查Oracle服务进程,检查Oracle监听进程,共三个部分. 1.1. 检查Oracle实例状态 select instance_name ...

  2. .Net 异步方法加上“timeout”

    在本羊读大学的时候,Thread让本羊云里雾里,代码写的痛不欲生,真的是让本羊脑袋里很多“线”缠绕在一起. 之后,Task让本羊代码写的飞起,甚至有时候根本不需要Task的时候还是要写上,那样显得档次 ...

  3. HBase配置项详解

    hbase.tmp.dir:本地文件系统的临时目录,默认是java.io.tmpdir/hbase−java.io.tmpdir/hbase−{user.name}: hbase.rootdir:hb ...

  4. ASP.NET - SqlSugar ORM框架 更新列表

    以后SqlSugar所有更新都会在这个贴子更新 SqlSugar是一款轻量级的MSSQL ORM ,除了具有媲美ADO的性能外还具有和EF相似简单易用的语法. 学习列表 0.功能更新 1.SqlSug ...

  5. 使用axes函数在matlab绘图中实现图中图的绘制

    使用axes函数在matlab绘图中实现图中图的绘制 有时为了对细节进行详细说明,需要在一个较大坐标轴上绘制一个小图来对局部进行放大以阐述结果. 这可以通过调用axes函数实现. 下面通过绘制 y=1 ...

  6. jaccard similarity coefficient 相似度计算

    Jaccard index From Wikipedia, the free encyclopedia     The Jaccard index, also known as the Jaccard ...

  7. RabbitMQ官方中文入门教程(PHP版) 第四部分:路由(Routing)

    路由(Routing) 在前面的教程中,我们实现了一个简单的日志系统.可以把日志消息广播给多个接收者. 本篇教程中我们打算新增一个功能——使得它能够只订阅消息的一个字集.例如,我们只需要把严重的错误日 ...

  8. 软件工程(FZU2015)学生博客列表(最终版)

    FZU:福州大学软件工程 张老师的博客:http://www.cnblogs.com/easteast/ 经过前两周选课,最后正式选上课程的所有学生博客如下: 序号 学号后3位 博客 1 629 li ...

  9. springMvc接受日期类型参数处理

    这个问题,也即是springMvc如何进行参数类型的转换 以把client传过来一个String类型,转换为日期类型为例: 1.controller /** * 接收日期类型参数 * 注意: * sp ...

  10. truncate与delete的区别

    TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行.但 TRUNCATE TABLE 比 DELETE 速度快,且使用的系统和事务日志资源 ...