HDOJ/HDU 1022 Train Problem I(模拟栈)

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");
        }
    }
}
时间: 2024-11-27 21:01:02

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

算法题:HDU 1022 Train Problem I 栈

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

C - Train Problem II——(HDU 1023 Catalan 数)

传送门 Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7616    Accepted Submission(s): 4101 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train

HDU 1022

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10829    Accepted Submission(s): 3924 Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays

HDOJ/HDU 5686 Problem B(斐波拉契+大数~)

Problem Description 度熊面前有一个全是由1构成的字符串,被称为全1序列.你可以合并任意相邻的两个1,从而形成一个新的序列.对于给定的一个全1序列,请计算根据以上方法,可以构成多少种不同的序列. Input 这里包括多组测试数据,每组测试数据包含一个正整数N,代表全1序列的长度. 1≤N≤200 Output 对于每组测试数据,输出一个整数,代表由题目中所给定的全1序列所能形成的新序列的数量. Sample Input 1 3 5 Sample Output 1 3 8 Hin

HDOJ(HDU) 1898 Sempr == The Best Problem Solver?(水题、、、)

Problem Description As is known to all, Sempr(Liangjing Wang) had solved more than 1400 problems on POJ, but nobody know the days and nights he had spent on solving problems. Xiangsanzi(Chen Zhou) was a perfect problem solver too. Now this is a story

HDOJ(HDU) 2123 An easy problem(简单题...)

Problem Description In this problem you need to make a multiply table of N * N ,just like the sample out. The element in the ith row and jth column should be the product(乘积) of i and j. Input The first line of input is an integer C which indicate the

HDOJ(HDU) 2132 An easy problem

Problem Description We once did a lot of recursional problem . I think some of them is easy for you and some if hard for you. Now there is a very easy problem . I think you can AC it. We can define sum(n) as follow: if i can be divided exactly by 3 s

Train Problem I

http://acm.hdu.edu.cn/showproblem.php?pid=1022 栈的模板题. 1 #include<iostream> 2 #include<cstdio> 3 #include<stack> 4 #include<string> 5 #include<cstring> 6 using namespace std; 7 string a,b; 8 int c[100]; 9 int main() 10 { 11 //

数据结构实践——停车场模拟(栈和队列综合)

本文是针对数据结构基础系列网络课程(3):栈和队列的实践项目. 设停车场是一个可停放n辆汽车的狭长死胡同,南边封口,汽车只能从北边进出(这样的停车场世间少有).汽车在停车场内按车辆到达时间的先后顺序,最先到达的第一辆车停放在车场的最南端,依次向北排开.若车场内已停满n辆汽车,则后来的汽车只能在门外的候车场上等候,一旦有车开走,则排在候车场上的第一辆车即可开入.当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路(假定停车场内设有供车辆进出的便道,所有的司机也必须在车内随时待命),待