Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24377    Accepted Submission(s): 9196

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

Hint

Hint

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

 
代码:
 #include<stdio.h>
#include<cstring>
using namespace std;
int main()
{
char o1[],o2[],stack[];
int n,A=,B=,top=,inout[],flag=,ok; //用inout数组来保存压栈和出栈,1为压,0为出
while(scanf("%d %s %s",&n,o1,o2)!=EOF)
{
memset(inout,,sizeof(inout));
ok=;
A=;
B=;
flag=;
top=;
while(B<=n)
{
if(o1[A]==o2[B]) //当前进栈的车与目标顺序中当前列车一致,即进站后立即出站
{
A++;
B++;
inout[flag++]=;
}
else if(top&&stack[top]==o2[B]) //上一种情况不符合,栈顶的列车与目标顺序当前列车一致,即只需出站
{
top--;
B++;
inout[flag++]=;
}
else if(A<=n) // 上两种情况均不符合,只需要压栈
{
stack[++top]=o1[A++];
inout[flag++]=;
}
else //上述三种情况均不符合,此时A>n
{
ok=;
break;
} }
if(ok)
{
printf("Yes.\n");
for(int j=;j<flag-;j++)
if(inout [j]==)
printf("in\nout\n");
else if(inout[j]==)
printf("in\n");
else if(inout[j]==)
printf("out\n");
printf("FINISH\n");
}
else
printf("No.\nFINISH\n");
}
}

最新文章

  1. [原创]MySQL RR隔离级别下begin或start transaction开启事务后的可重复读?
  2. 最全的jQuery知识汇总
  3. jq checkbox 的全选并ajax传参
  4. mysql metadata lock(一)
  5. iOS之单例
  6. cocos基础教程(9)声音和音效
  7. unity3d用鼠标拖动物体的一段代码
  8. js实现网站导航的二级下拉菜单
  9. Cocos2D创建项目
  10. Windows Phone 8 开发环境搭建
  11. 【转】Xcode7.1环境下上架iOS App到AppStore 流程 (Part 三)
  12. Filter及FilterChain的使用详解
  13. effective c++ 条款13 use object to manage resources.
  14. python_大学排名爬取
  15. mysql8.0版本修改密码
  16. 升级到 .NET Core 2.1
  17. socket编程(C++)
  18. 《Apache kafka实战》读书笔记-管理Kafka集群安全之ACL篇
  19. oracle对sum出来的数字进行非空补0处理
  20. Could not determine own NN ID in namespace &#39;mycluster&#39;

热门文章

  1. 【Linux多线程】三个经典同步问题
  2. kvm 安装
  3. JSON介绍及Android最全面解析方法(Gson、AS自带org.son、Jackson解析)
  4. HDU5294 Tricks Device(最大流+SPFA) 2015 Multi-University Training Contest 1
  5. CSS总结01
  6. Delphi各销售版本之间的区别
  7. [RK3288][Android6.0] 调试笔记 --- 录音音量从HAL到APP层会变小问题【转】
  8. 【JSOI 2008】 最小生成树计数
  9. vue的学习网址
  10. 15.extjs tabPanel的用法