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");
}
}

HDU_1022_Train Problem I的更多相关文章

  1. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  2. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  3. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  4. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  5. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  6. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  8. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  9. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

随机推荐

  1. 转:js点击事件在ios中失效的3种解决方案

    ios中不允许将点击事件绑定在document或者body上,如果绑定上的话将会失效.解决方案: 例如:  $(document).on('click', '#generate', function ...

  2. nyoj 739 笨蛋的难题四

    笨蛋难题四 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 这些日子笨蛋一直研究股票,经过调研.最终发现xxx公司股票规律,更可喜的是 笨蛋推算出这家公司每天的股价, ...

  3. 怎样在gluster的源代码中加入自己的xlator

    本文并不说明怎样编写一个xlator.在glusterfs3.6.1下验证成功 目标在glusterfs-3.6.1/xlators/debug/下建立一个自己的xlator库并能够统一安装到系统文件 ...

  4. hdu1162

    #include<cstdio> #include<cmath> #include<climits> #include<algorithm> #defi ...

  5. js全局替换空格,制表符,换行符

    this.value = this.value.replace(/\s+/g,'') "/ "这个是固定写法, "\s"匹配任何不可见字符,包括空格.制表符.换 ...

  6. [翻译角]Headline English: A Captain Should Be Pitch Perfect at a Multitude of Skills (ESLPOD Blog)

    以下转自www.eslpod.com,翻译为本人添加.其余版权均归原网站所有. ESLPOD是一个英语学习网站,我最初知道这个网站,是因为“奶爸”<把你的英语用起来>(电子版3元)一书的推 ...

  7. 解决Eclipse alt+/不出来提示的问题

    1. 检查windows ——preferences ——java ——editor —— content assist - advanced,在右上方有一行“select the proposal ...

  8. git log 常用选项

    git log 常用选项 -p 按补丁格式显示每个更新之间的差异. jiqing@Ubuntu:/home/wwwroot/default/5hao/sheep$ git log -p commit ...

  9. Tomcat + solr5.2.1环境搭建

    1. 下载solr并解压后的目录为:E:\solr-5.2.1   ,  http://lucene.apache.org/solr/downloads.html 2. 将solr部署到Tomcat中 ...

  10. Netty,Thrifty

    小白科普:Netty有什么用? https://mp.weixin.qq.com/s/PTKnRQ_hLf8BBPYnywLenA Thrifty 是基于.net Attribute 实现了标准 Th ...