Train Problem I

Train
Problem I


Time
Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K
(Java/Other)

Total
Submission(s) : 35   Accepted Submission(s) : 14

Font: Times New Roman | Verdana | Georgia


Font
Size: ← →


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

Hint

Hint 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.".

Author

Ignatius.L

 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 int main()
6 {
7 int NUM,Len,i,j,k,sign,Top;
8 char str[10000],flat[10000],FLAT[10000],SIGN[10000];
9 while(scanf("%d",&NUM)!=EOF)
10 {
11 memset(flat,0,sizeof(flat));
12 memset(FLAT,0,sizeof(FLAT));
13 memset(FLAT,0,sizeof(FLAT));
14 getchar();
15 scanf("%s%s",flat,FLAT);
16 for(i=0,j=0,k=0,sign=0,Top=0;k<=NUM&&j<=NUM;)
17 {
18 if(Top>=0&&k>0&&str[Top]==FLAT[j])
19 {
20 str[Top]=‘\0‘;
21 Top-=1;k-=1;j++;
22 if(Top==0&&j>=NUM)break;
23 SIGN[sign++]=1;
24 }
25 else
26 {str[k++]=flat[i++];Top=k-1;SIGN[sign++]=0;}
27 str[k]=‘\0‘;
28 }
29 str[k]=‘\0‘;
30 if(str[0]==‘\0‘)
31 {
32 printf("Yes.\n");
33 for(i=0;i<sign-2;i++)
34 if(SIGN[i]==0)
35 printf("in\n");
36 else if(SIGN[i]==1)
37 printf("out\n");
38
39 }
40 else
41 printf("No.\n");
42 printf("FINISH\n");
43 }
44
45
46
47 }

Train Problem I,布布扣,bubuko.com

时间: 2024-12-15 06:41:23

Train Problem I的相关文章

HDU1022 Train Problem I (栈)

栈+队列 1 #include<stdio.h> 2 #include<string.h> 3 #include<stack> 4 #include<queue> 5 using namespace std; 6 int main() 7 { 8 int n; 9 char a[11],b[11]; 10 stack<char>s; 11 queue<int>q; 12 while(scanf("%d",&

hdu 1022 Train Problem

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

Train Problem II 卡特兰裸题(入门题)

Train Problem II  题目大意:给你一个数n,表示有n辆火车,编号从1到n,从远方驶过来,问你有多少种出站的可能. 解题思路:模拟栈的问题而已.  卡特兰问题. 1 import java.math.*; 2 import java.util.*; 3 import java.io.*; 4 5 public class Main 6 { 7 static int MS=101; 8 public static void main(String[] args) 9 { 10 Sca

hdoj 1023 Train Problem II 【卡特兰】+【高精度】

题意:询问有多少种进站出站的顺序. 经典卡特兰.我对卡特兰目前的认识就是有n个1和n个-1,组成一个为2n的数列的方式有多少种.这就跟火车进站出站类似, 至于具体的卡特兰数的介绍,百度解释的很详细. 代码1(c语言): /* h(n) = h(n-1)*(4*n-2)/(n+1); */ #include <stdio.h> #include <string.h> #define M 110 int s[M][M] = {0}, b[M]; void init(){ s[1][0]

Train Problem II HDU 1023 卡特兰数

Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway. Input The input contains se

hdoj 1022 Train Problem I 【简易STL】

题意:不解释(这题是学数据结构必做的) 以前自学数据结构的时候,只是会顺序表来模拟栈.最近简单学习了stack头文件 又来做了一遍(还是以前的味道) 代码: #include <stdio.h> #include <stack> #include <string.h> using std::stack; stack<char > s; char s1[100], s2[100]; int vis[10]; char stac[100]; int main()

Train Problem II (卡特兰数+大数问题)

卡特兰数: Catalan数 原理: 令h(1)=1,h(0)=1,catalan数满足递归式: h(n)= h(1)*h(n-1) + h(2)*h(n-2) + ... + h(n-1)h(1) (其中n>=2) 另类递归式: h(n)=((4*n-2)/(n+1))*h(n-1); 该递推关系的解为: h(n+1)=C(2n,n)/(n+1) (n=1,2,3,...) 最典型的四类应用:(实质上却都一样,无非是递归等式的应用,就看你能不能分解问题写出递归式了) 1.括号化问题. 矩阵链乘

train problem I (栈水题)

杭电1002http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25276    Accepted Submission(s): 9529 Problem Description As the new term com

HDU 1022 之 Train Problem I

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

hdu1022 train problem 栈的应用

#include #include #include using namespace std; int main() { int n; while(cin >> n) { stack one; string od1,od2; bool state[10001]; cin >> od1 >> od2; int from = 0 , to = 0; int i = 0; while(from < n) { while(od1[from] != od2[to]) one