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. HttpClient4.3 使用经验(一) 简单使用

    package com.wp.nevel.base.utils; import java.io.BufferedOutputStream; import java.io.BufferedReader; ...

  2. IOS 学习笔记 2015-03-22 OC-API-日期

    一 API 1 NSdate 2 NSDateFormatter 二 适用场景 1 获取当前日期 2 增加时间差 3 比较时间差 4 返回较早时间 5 日期格式话 6 日期转字符串 7 字符串转日期 ...

  3. nginx详细配置文件 (转)

    Nginx的代码是由一个核心和一系列的模块组成, 核心主要用于提供Web Server的基本功能,以及Web和Mail反向代理的功能:还用于启用网络协议,创建必要的运行时环境以及确保不同的模块之间平滑 ...

  4. win7下简单FTP服务器搭建

    本文介绍通过win7自带的IIS来搭建一个只能实现基本功能的FTP服务器,第一次装好WIN7后我愣是没整出来,后来查了一下网上资料经过试验后搭建成功,其实原理和步骤与windows前期的版本差不多,主 ...

  5. SQL Function(方法)

    1.为什么有存储过程(procedure)还需要(Function) fun可以再select语句中直接调用,存储过程是不行的. 一般来说,过程显示的业务更为复杂:函数比较有针对性. create f ...

  6. Building Python 2.7.10 with Visual Studio 2010 or 2015 - Google Chrome

    您的浏览器(Chrome 33) 需要更新.该浏览器有诸多安全漏洞,无法显示本网站的所有功能. 了解如何更新浏览器 × p-nand-q.com C++  Python  Programming  L ...

  7. 使用WMI监控进程启动与结束

    需要添加引用System.Management 代码: static void Main(string[] args) { //创建WQL事件查询,监视进程开启 var qCreate = new W ...

  8. iBatis系列一

    XML iBatis可以使用xml来作为参数输入以及结果返回:这个功能的优势在于某些特定的场景:还有可以通过DOM方式来作为参数传递:但是这个方式应用的比较少,如果服务器是xml服务器可以采用这种方式 ...

  9. PHP之路——大文件上传

    修改php.ini 1.file_uploads=on/off 是否允许通过http方式上传文件 2.max_execution_time=30 允许脚本最大执行时间,超过这个时间就会报错 3.upl ...

  10. java+eclipse+tomcat+mysql+jdbc——完美配置攻略

    说明: 软件均采用最新版本,请大家详细阅读,注意每个细节,无需分门别类的百度各种教程,配置java环境这一篇就够了. 所需软件及版本(参考): java8; - jdk1.8.0_60; - jre1 ...