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>


目录
相关文章
|
监控 关系型数据库 MySQL
MySQL创建索引的注意事项
在数据库设计和优化中,索引的合理使用是提高查询性能和加速数据检索的关键因素之一。通过选择适当的列、了解数据分布、定期维护和监控索引性能,我们能够最大程度地发挥索引的优势,提高数据库的效率和响应速度。
360 0
|
存储 设计模式 缓存
通用点赞设计思路
点赞作为一个高频率的操作,如果每次操作都读写数据库会增加数据库的压力,所以采用缓存+定时任务来实现。点赞数据是在redis中缓存半小时,同时定时任务是每隔5分钟执行一次,做持久化存储,这里的缓存时间和任务执行时间可根据项目情况而定。
2693 2
|
缓存 NoSQL 数据库
探秘Redis读写策略:CacheAside、读写穿透、异步写入
本文介绍了 Redis 的三种高可用性读写模式:CacheAside、Read/Write Through 和 Write Behind Caching。CacheAside 简单易用,但可能引发数据不一致;Read/Write Through 保证数据一致性,但性能可能受限于数据库;Write Behind Caching 提高写入性能,但有数据丢失风险。开发者应根据业务需求选择合适模式。
1809 2
探秘Redis读写策略:CacheAside、读写穿透、异步写入
|
NoSQL 关系型数据库 MySQL
排行榜系统设计:高并发场景下的最佳实践
本文由技术分享者小米带来,详细介绍了如何设计一个高效、稳定且易扩展的排行榜系统。内容涵盖项目背景、技术选型、数据结构设计、基本操作实现、分页显示、持久化与数据恢复,以及高并发下的性能优化策略。通过Redis与MySQL的结合,确保了排行榜的实时性和可靠性。适合对排行榜设计感兴趣的技术人员参考学习。
1592 7
排行榜系统设计:高并发场景下的最佳实践
|
存储 运维 Dubbo
SAE急速部署
Serverless应用引擎 2.0的推出,您是否用过?更快速的部署,更低的成本,SAE 2.0 极简体验、极致弹性,助力企业降本增效!
10170 14
SAE急速部署
|
SQL 存储 关系型数据库
MySQL主从同步延迟原因与解决方法
MySQL主从同步延迟原因与解决方法
1366 0
|
人工智能 算法 BI
【经典问题】给两个文件,分别有100亿个整数,我们只有1G内存,如何找到两个文件交集?
【1月更文挑战第26天】【经典问题】给两个文件,分别有100亿个整数,我们只有1G内存,如何找到两个文件交集?
|
存储 搜索推荐 算法
“插入排序:小数据量排序的王者“
“插入排序:小数据量排序的王者“
295 0
|
机器学习/深度学习 Web App开发 数据挖掘
经典神经网络论文超详细解读(七)——SENet(注意力机制)学习笔记(翻译+精读+代码复现)
经典神经网络论文超详细解读(七)——SENet(注意力机制)学习笔记(翻译+精读+代码复现)
4143 1
经典神经网络论文超详细解读(七)——SENet(注意力机制)学习笔记(翻译+精读+代码复现)