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. android意图传參数(四)

    一.依照向导创建一个project,layout的activity_main.xml文件内容例如以下: <RelativeLayout xmlns:android="http://sc ...

  2. C++进阶之虚函数表

    C++通过继承(inheritance)和虚函数(virtual function)来实现多态性.所谓多态,简单地说就是,将基类的指针或引用绑定到子类的实例,然后通过基类的指针或引用调用实际子类的成员 ...

  3. linux下alias命令具体解释

    linux下alias命令具体解释 用途说明 设置命令的别名.在linux系统中假设命令太长又不符合用户的习惯,那么我们能够为它指定一个别名. 尽管能够为命令建立"链接" 解决长文 ...

  4. Android多媒体-MediaPlayer唤醒锁及音频焦点

    MediaPlayer的唤醒锁 一般使用MediaPlayer播放音频流,推荐使用一个Service来承载MediaPlayer,而不是直接在Activity里使用.可是Android系统的功耗设计里 ...

  5. [办公应用]如何制作二Y轴图(excel)

    有时候我们会遇到一种图表,就是X轴一致,可是Y轴的数据相差很大.如下图中,年龄和收入就不是一个数量级,在图表中显示的时候,“年龄”的曲线根本看不到(表中数据仅供举例): 解决的方法就是使用双Y轴显示, ...

  6. 图片存储系统TFS

    1 TFS和GFS比较 1.1 GFS的应用场景 第一,百万级别的文件,并且是大文件,文件都是100MB以上,1G级别的文件很常见. 第二,集群是建立在商业计算机之上,并不可靠,监控各个节点的状态,当 ...

  7. Delphi属性比对象的域有更强的功能

    8.4 Delphi自定义组件(3) http://tech.163.com 2006-04-29 11:49:34 来源: 清华大学出版社 网友评论0 条 论坛   3. 测试未安装的组件 在将新组 ...

  8. [DevExpress]DevExpress的安装与使用

    一.下载安装文件 依据自己的须要选择不同的版本号.下面为15.1 安装时选择自己须要的模块进行安装,之后进行激活,购买授权或者"其它方式". 二.安装完 在VSIDE工具栏会添加下 ...

  9. 蓝桥 ADV-230 算法提高 12-1三角形 【数学公式】

      算法提高 12-1三角形   时间限制:1.0s   内存限制:256.0MB      问题描述 为二维空间中的点设计一个结构体,在此基础上为三角形设计一个结构体.分别设计独立的函数计算三角形的 ...

  10. 编译android的一些坑

    1 降级gcc g++到4.4 2 参考:http://source.android.com/source/initializing.html来配置环境 3 使用jdk1.6 包括 java java ...