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 Test5 {
  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 }
时间: 2024-10-03 13:10:09

hdu 1022火车进出站问题的相关文章

HDU 1022(火车过站 栈)

题意是给定火车进站的序列和出站的序列,问能否完成转换,若能输出过程. 和另一道以火车进站为背景的栈应用题类似,但增加了对于过程的输出,只需要多记录一下进出站顺序即可. 1 #include <bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 std::ios::sync_with_stdio(false); 6 int n,pos,cnt,vis[1000]; 7 stack<char> q; 8 char come[

火车进出站(POJ1363)

题目链接:http://poj.org/problem?id=1363 #include <stdio.h> #include <stack> using namespace std; int ans[10000]; ///出栈秩序 int main() { int t; ///有多少车厢 while(scanf("%d",&t),t) { stack<int>s; while(scanf("%d",&ans[0]

HDU 1022 (STL_D题)解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 ----------------------------------------------------------------------------------- 题意:火车进出站,可看作不同的数字的按一定顺序进栈,能否按所要求的顺序出栈. 思路:将出栈顺序保留着数组中,进栈顺序,只要目前的元素不为出栈顺序的数组的值,就进栈.如123 321.因为1不等于3,进栈.2不等于3,进栈.3等于

HDU 1022 Train Problem I 用栈瞎搞

题目大意:有n辆火车,按一定的顺序进站(第一个字符串顺序),问是否能按规定的顺序出站(按第二个字符串的顺序出去),如果能输出每辆火车进出站的过程. 题目思路:栈的特点是先进后出,和题意类似,还有有一种情况是:开进来立马有开出去.并用vis[]数组的0,1标记进出站情况. 具体看代码 #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<algorithm&

HDU 1022 Train Problem I (STL 栈模拟)

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

HDU 1022 Train Problem I 模拟栈题解

火车进站,模拟一个栈的操作,额外的栈操作,查看是否能按照规定顺序出栈. 数据量很少,故此题目很容易AC. 直接使用数组模拟就好. #include <stdio.h> const int MAX_N = 10; char inOrder[MAX_N], outOrder[MAX_N], stk[MAX_N]; bool rs[MAX_N<<2]; int n; int main() { while (scanf("%d", &n) != EOF) { s

HDU 1022 Train Problem I stack 基础题

题意: 有一个火车站,只有一个口同时用于进入和出去,但不能有2辆或以上同时进出. 给出一个n代表有n辆火车(n<=9),编号分别为1到n. 然后给出2个序列,分别代表火车进站的顺序,火车出站的顺序. 问按照这个进站的顺序,能不能按照这个出站的顺序出站. 若能,输出这个口进出的顺序. Sample Input 3 123 321 3 123 312 Sample Output Yes. in in in out out out FINISH No. FINISH 直接用stack进行模拟就OK了.

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

火车出站

火车出站 题目描述 铁路进行列车调度时,常把站台设计成栈式结构的站台,试问:设有编号为1到n的n辆列车,顺序开入栈式结构的站台,则可能的出栈序列有多少种? 输入 输入包含多组测试数据.每组为一个正整数n(1<=n<=20),表示有n辆列车. 输出 输出可能的出栈序列有多少种. 样例输入 4 3 样例输出 14 5 1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 int n; 6 while(cin>