zoukankan      html  css  js  c++  java
  • hdu 1022火车进出站问题

    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
     
    题意:
     
      1 import java.util.Scanner;
      2 import java.util.Stack;
      3 
      4 public class Main{
      5     
      6      static Stack<Character> s;
      7      static char[] in,out;
      8      static String[] res;
      9      static String str1,str2;
     10      static int n;
     11      static int count1,count2;
     12 
     13     public static void main(String[] args) {
     14 
     15         Scanner input=new Scanner(System.in);
     16         
     17         while(input.hasNext()) {
     18             
     19             n=input.nextInt();
     20             
     21             s=new Stack<Character>();
     22             in=new char[n+1];//火车进站顺序
     23             out=new char[n+1];//火车出站顺序
     24             res=new String[2*n+1];//每个火车进出站记录
     25             
     26             str1=input.next();//火车进站序列
     27             str2=input.next();//火车出站序列
     28             
     29             for(int i=0;i<n;i++) {
     30                 
     31                 in[i]=str1.charAt(i);
     32                 out[i]=str2.charAt(i);
     33                 
     34             }
     35             
     36             count1=0;//进站的列车序号
     37             count2=0;//出站的列车序号
     38             boolean istrue=dfs(0);
     39             
     40             if(istrue) {
     41                 
     42                 System.out.println("Yes.");
     43                 for(int i=0;i<2*n;i++) {
     44                     
     45                     System.out.println(res[i]);
     46                     
     47                 }
     48                 
     49             }else {
     50                 
     51                 System.out.println("No.");
     52                 
     53             }
     54             
     55             System.out.println("FINISH");
     56             
     57         }
     58 
     59     }
     60 
     61     //判断是否可以按str2顺序出站
     62     private static boolean dfs(int k) {
     63         
     64         if(count2==n) {//若出站列车序号为n时,说明前面所有的列车都可按照str2顺序出站
     65             
     66             return true;
     67             
     68         }
     69         
     70         if(k==n&&count2<n) {//当进站列车序号为n,说名所有火车都已经进站,count2<n说明出站火车列表中还有火车没有出站
     71             
     72             return false;
     73             
     74         }
     75         
     76         if(in[k]!=out[count2]) {//若序号为k的进站列车和出站列车序号不相等,则让序号为k的进站列车进站;然后判断下一个进站列车情况
     77             
     78             s.push(in[k]);
     79             res[count1++]="in";
     80             return dfs(k+1);
     81             
     82         }else {//(1)若序号为k的进站列车与序号为count2的出站列车序号相等,则让k先进站,然后立即出站;并将count2++(下一个该出站的列车)
     83             
     84             //(2)然后判断栈定列车序号是否和出站列车序号是否相等,若相等将栈顶列车出站并做记录,然后将count2++(下一个该出站的列车),重复步骤2直到栈为空或者栈顶火车与count2(下一个该出站的火车)不相等,否则判断下一个火车情况
     85             res[count1++]="in";
     86             res[count1++]="out";
     87             count2++;
     88             while(!s.isEmpty()) {
     89                 
     90                 if(s.peek()!=out[count2]) {
     91                     
     92                     break;
     93                     
     94                 }
     95                 
     96                 res[count1++]="out";
     97                 s.pop();
     98                 count2++;
     99             }
    100             
    101             return dfs(k+1);
    102             
    103         }
    104         
    105         
    106     }
    107 
    108 }
  • 相关阅读:
    相信未来 ————11月份做题记录
    noi 滚cu后7月oi生活
    博客已经迁移到 http://imbotao.top 也会同步到这儿
    构建第一个SpringBoot工程
    Spring 事务管理
    IntelliJ IDEA 使用技巧
    JS 获取字符串实际长度
    读《程序员修炼之道》
    读《如何高效学习》
    SQL 语句优化方法
  • 原文地址:https://www.cnblogs.com/xuzhiyuan/p/7845435.html
Copyright © 2011-2022 走看看