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

HintHint

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



火车进站问题,输入首先包含一个整数N,表示要进站的火车数,然后是依次输入进站火车的序号,然后再输入出站火车的序号,看是否符合出站顺序。

当符合顺序的时候输出”Yes.”依次输入每趟车进站出站的状态进站表示”in” 出站表示”out”,

当不符合顺序的时候输出”No.”程序结束后输出”FINISH”

分析:

模拟栈的输入输出!

import java.util.Scanner;

public class Main{

    public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int t = sc.nextInt();
String in = sc.next();
String out = sc.next();
int a=0;
int b=0;
boolean ok=true;
int top=0;//表示栈里面还有元素
char stack[] = new char[50];
boolean flag[] = new boolean[50];
int i=0;
while(b<t){
if(top!=0 && ( stack[top]==out.charAt(b) )){
top--;
b++;
flag[i]=false;
i++;
}else if(a<t && in.charAt(a)==out.charAt(b)){
a++;
b++;
flag[i]=true;
i++;
flag[i]=false;
i++;
}else if(a<t){
top++;
stack[top]=in.charAt(a);
a++;
flag[i]=true;
i++;
}else{
ok=false;
break;
}
}
if(ok){
System.out.println("Yes.");
for(int k=0;k<i;k++){
System.out.println( flag[k]?"in":"out" );
}
}else{
System.out.println("No.");
} System.out.println("FINISH");
}
}
}

HDOJ/HDU 1022 Train Problem I(模拟栈)的更多相关文章

  1. HDU 1022 Train Problem I 模拟栈题解

    火车进站,模拟一个栈的操作,额外的栈操作,查看能否依照规定顺序出栈. 数据量非常少,故此题目非常easyAC. 直接使用数组模拟就好. #include <stdio.h> const i ...

  2. HDU 1022 Train Problem I(栈的操作规则)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Ot ...

  3. hdu 1022 Train Problem I(栈的应用+STL)

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. HDU 1022.Train Problem I【栈的应用】【8月19】

    Train Problem I Problem Description As the new term comes, the Ignatius Train Station is very busy n ...

  5. HDU 1022 Train Problem I(栈模拟)

    传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

  6. hdu 1022 Train Problem I(栈)

    #include<iostream> #include<vector> #include<cstring> #include<string> #incl ...

  7. HDU 1022 Train Problem I 用栈瞎搞

    题目大意:有n辆火车,按一定的顺序进站(第一个字符串顺序),问是否能按规定的顺序出站(按第二个字符串的顺序出去),如果能输出每辆火车进出站的过程. 题目思路:栈的特点是先进后出,和题意类似,还有有一种 ...

  8. hdu 1022 Train Problem I【模拟出入栈】

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. HDU - 1022 Train Problem I STL 压栈

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. ZOJ 2745 01-K Code(DP)(转)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1745 题目大意:一个串由N个字符组成,每个字符是‘0’或者是‘1’, ...

  2. Object-API-NSLog

    NSLog中的基础数据类型 输出格式: NSLog("") char: %c short int: %hi %hx %ho unsigned short int: %hu %hx ...

  3. CSS Masking(翻译)

    原文地址:http://www.html5rocks.com/en/tutorials/masking/adobe/ 关于计算机图形,两种常见的操作是:cliping(裁剪) and  masking ...

  4. react组件什么周期记录,转的

    react 的核心除了虚拟DOM,我想还有一个很重要的就是生命周期函数,理解生命周期函数,对写出合理的commponet很有帮助.下面总结一下我对生命周期函数的一些理解已经在项目过程中遇到的一些问题. ...

  5. NetSerialComm的基本使用方法

    近期搞一个com口传输的小项目,原来认为是一个挺简单的一个小功能,结果生产商发来com以后直接傻眼了,还要对相关的硬件流进行处理 如下 // 硬件流控制设置 dcb.fOutxCtsFlow = FA ...

  6. 解决修改SQL SERVER 默认1433端口 访问出错的问题;

    1. 如何修改 数据库默认的 1433端口: SQL SERVER 配置管理器中 SQLSERVER 网络配置 xxx数据实例的协议中的  TCP/IP 中 默认端口 都修改为 自己定义的端口 例如 ...

  7. DIV+CSS 网页布局之:混合布局

    1.混合布局 在了解了一列.两列和三列布局之后,混合布局也就不难理解了,混合布局也可以叫综合型布局,那么混合布局就可以在一列布局的基础之上,分为两列布局,三列布局,网页布局的结构普遍都是三列布局,但是 ...

  8. python 循环while和for in

    #!/uer/bin/env python # _*_ coding: utf-8 _*_ lucknumber = 5 b = 0 while b <3: print('guss count: ...

  9. NET Core 整合Autofac和Castle

    NET Core 整合Autofac和Castle 阅读目录 前言: 1.ASP.NET Core中的Autofac 2.整合Castle的DynamicProxy 3.注意事项 回到目录 前言: 除 ...

  10. Linux发行版

    Linux 发行版(英语:Linux distribution,也被叫做GNU/Linux 发行版),为一般用户预先集成好的Linux操作系统及各种应用软件.一般用户不需要重新编译,在直接安装之后,只 ...