Train Problem I

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
  1. 3 123 321
  2. 3 123 312
 
Sample Output
  1. Yes.
  2. in
  3. in
  4. in
  5. out
  6. out
  7. out
  8. FINISH
  9. No.
  10. FINISH
  11. Hint
  12. Hint
  13. For the first Sample Input, we let train 1 get in, then train 2 and train 3.
  14. So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1.
  15. 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.
  16. Now we can let train 3 leave.
  17. 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.
  18. So we output "No.".
  19.  

栈的应用:给定1个出栈序列,看当前入栈序列是否能通过入栈出栈达到这个出栈序列.代码例如以下:

  1. #include<cstdio>
  2. #include<stack>
  3. using namespace std;
  4. int main(){
  5. int n;
  6. char f1[1010],f2[1010];//f1记录入栈顺序,f2记录出栈顺序
  7. while(scanf("%d%s%s",&n,f1,f2)!=EOF){
  8. int g[1010],i=0,j=0,k=0;//g[]记录进出情况
  9. stack<char> t;
  10. while(i<n){
  11. if(t.empty()||t.top()!=f2[i]&&j<n){
  12. t.push(f1[j++]);
  13. g[k++]=1;//1为in
  14. }
  15. else if(t.top()==f2[i]){
  16. t.pop();
  17. g[k++]=0;//0为out
  18. i++;
  19. }
  20. else break;
  21. }
  22. if(t.empty()){//栈t为空,则能够生成当前出栈序列
  23. printf("Yes.\n");
  24. for(int i=0;i<k;i++){
  25. if(g[i]) printf("in\n");
  26. else printf("out\n");
  27. }
  28. printf("FINISH\n");
  29. }
  30. else printf("No.\nFINISH\n");
  31. }
  32. return 0;
  33. }

HDU 1022.Train Problem I【栈的应用】【8月19】的更多相关文章

  1. Hdu 1022 Train Problem I 栈

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

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

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

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

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

  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(数据结构,栈,递归,dfs)

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

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

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

  8. HDU 1022 Train Problem I

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

  9. hdu 1022 Train Problem

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

随机推荐

  1. 有了这些,java IO就不愁了

    IO的总结: java中相对路径和绝对路径的问题: 在web项目中,如果生成的文件前面没有 / 开头的话,表示的是生成的文件在当前项目的根目录下如student.txt在项目中刷新就能看到. 如果是以 ...

  2. ESP8266基础篇

    ESP8266基础篇 模块刚到如图所示~2016-01-26 后三张是手机APP的截图,前面是两个ESP8266的硬件模块,当然大家不应定要买两个,自己焊锡一个USB转TTL的串口就行了,有了下面的底 ...

  3. XX-NET史上最详细完整教程

     转 https://www.cnblogs.com/phperkang/p/8780123.html 前言 XX-NET,系GAE类代理,即通过可用Google ip连接Google App Eng ...

  4. POJ 2001 Shortest Prefixes 【Trie树】

    <题目链接> 题目大意: 找出能唯一标示一个字符串的最短前缀,如果找不出,就输出该字符串. 解题分析: Trie树的简单应用,对于每个单词的插入,都在相应字符对应的节点 num 值+1 , ...

  5. HDU 4614 Vases and Flowers 【线段树】+【二分】

    <题目链接> 题目大意: 有n个花瓶,每个花瓶中只能放一朵花.两种操作,一种是从A开始放F朵花,如果有的花瓶中已经有花则跳过这个花瓶,往下一个花瓶放:第二种是将区间[A,B]之间花瓶中的花 ...

  6. HDU1232——畅通工程【并查集】

    <题目链接> 题目大意: 利用并查集找出图中有几个不连通的城镇集合,所需修的道路数即为城镇集合-1. #include <stdio.h> ]; int find(int x) ...

  7. puppeteer 安装失败的解决方案

    你应该也是在玩puppeteer,所以才会来到这里寻找答案,开始正文…… 安装时遇到 ERROR: Failed to download Chromium r515411! Set "PUP ...

  8. sql语句1——join

    注:一下内容来自于http://www.w3school.com.cn/sql/ 1. 引用两个表 我们可以通过引用两个表的方式,从两个表中获取数据: 谁订购了产品,并且他们订购了什么产品? sele ...

  9. BZOJ.4821.[SDOI2017]相关分析(线段树)

    BZOJ LOJ 洛谷 恶心的拆式子..然后就是要维护\(\sum x_i,\ \sum y_i,\ \sum x_iy_i,\ \sum x_i^2\). 操作三可以看成初始化一遍,然后同操作二. ...

  10. 机器学习系列-tensorflow-03-线性回归Linear Regression

    利用tensorflow实现数据的线性回归 导入相关库 import tensorflow as tf import numpy import matplotlib.pyplot as plt rng ...