HDU-1022-火车进出站问题

简介: HDU-1022-火车进出站问题


<pre name="code" class="plain">


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. FINISH      



 


Sample Output

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



Sample Output

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

      题目意思是:      

      有一个车站   3 123  3是有三辆火车进站   123 分别是1 2 3号车   321是车辆开出顺序也就是列车必须是3号车出站后2号车才能出站之后是 1 号,当第 1 号辆进来的时候因为第一辆出战必须是3号车所以此时1号不能走,驶入2号,2号也不能走,驶入3号吗,3号可以走了,接着2号走,然后是1号走。      

      4 1234 2134  此时开出的顺序就不一样了。1号进来不能走,2号进来正好出车顺序是2134 所以 此时2号可以走了,2号走完正好1号走,1 号走完,3号进来,3号接着就出去了,最后是4号进4号出      

      这是在进站的时候就有出站情况的数据      

      /* 4 1234 2134      

Yes.
int
int
out
out
int
out
int
out
FINISH */


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
  int n;
  char c[15],g[15],zhan[15];
  int cg[20];
  while(cin>>n>>c>>g)
  {
    memset(cg,0,sizeof(cg));
    int i=-1,j=0,k=-1,x=-1;
    zhan[++k]=c[++i];// 先把第一个进来的车存入栈
    cg[++x]=0;// 进栈用 0 标记(便于一会输出出栈进栈顺序)
    while(c[i]!='\0')
    {
      while(k>=0&&zhan[k]==g[j])//   比如   进栈 123    出栈 321   可以在纸上演示一遍就明白这个循环了
      {
               cg[++x]=1;
               --k;
         ++j;
       }
                 cg[++x]=0;
           zhan[++k]=c[++i];
    }
    if(i==n&&j==n)
    {
      printf("Yes.\n");
      for(i=0;i<x;i++)
         puts(cg[i]?"out":"in");
    }
    else  puts("No.");
      puts("FINISH");
  }
  return 0;
}
</pre></div>


目录
相关文章
|
11月前
|
算法 Android开发 C++
LeetCode 周赛上分之旅 #49 再探内向基环树
学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越抽象,它能覆盖的问题域就越广,理解难度也更复杂。在这个专栏里,小彭与你分享每场 LeetCode 周赛的解题报告,一起体会上分之旅。
84 1
|
Cloud Native
【刷题日记】417. 太平洋大西洋水流问题
本次刷题日记的第 45 篇,力扣题为:417. 太平洋大西洋水流问题 ,中等
|
4月前
|
C++ 容器
[C++/PTA] 办事大厅排队
[C++/PTA] 办事大厅排队
53 0
|
容器 Cloud Native
【刷题日记】42. 接雨水
本次刷题日记的第 14 篇,力扣题为:42. 接雨水 ,困难
|
算法 C++
【每日算法Day 65】你能顺利救出地下城里的公主吗?
【每日算法Day 65】你能顺利救出地下城里的公主吗?
|
C++
201609-2 火车购票
201609-2 火车购票
92 0
201609-2 火车购票
|
算法
[leetcode] 787 K 站中转内最便宜的航班
题目大意: 给定一个有向图,从u → v 有一条长度为w 的边 问从s 到t 的路径中,最多经过k 次中转的最短路径是多少?
168 0
[leetcode] 787 K 站中转内最便宜的航班
|
算法
【完虐算法】LeetCode 接雨水问题,全复盘
恰巧前几天有一个粉丝问到了我,网上接雨水的解决总是感觉有点混乱,能不能用动态规划解决。 今早北京大雨,借用大雨的感受,想了想接雨水问题,依然用长图一步一步说明!
175 0
|
SQL XML 安全
拼搏百天我要日站——思路
第一步做的就是信息收集,正所谓知己知彼百战百胜,我们根据网站URL可以查出一系列关于该网站的信息。通过URL我们可以查到该网站的IP、该网站操作系统、脚本语言、在该服务器上是否还有其他网站等等一些列的信息。更多的关于信息收集,我在另一篇文章中很详细的介绍了信息收集需要收集哪些信息,以及信息收集过程中需要用到的工具,传送门——> 渗透测试之信息收集
128 0
|
程序员
阿里程序员深夜智救31楼跳楼邻居
“我妈跳楼了,快救救她!”。 8月20日凌晨的四点半左右,我被一阵急促的锤门声音吵醒。 听到这句话,没来得及思考,我就冲出了门。 我们住在31楼,出事的地点在邻居家的主卧,当时女主人整个人都悬在窗户外边,只有一只胳膊在窗户里边,房间里只有1个保安,正死命的拉住女主人的胳膊。
2425 0