G - Train Problem I(模拟题)

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.

这道题目首先要理解它的意思,它是一个类似于栈,但又有点不一样的。

题目意思是:

有两种命令IN,OUT,火车是按照IN的顺序进去的,要你判断能不能以OUT的方式出来;

这里要注意的是不要被样例所给的数据给忽悠了,例如123 321,有些时候并不完完全全是按照顺序输出的。

仔细想想 123 312 ,这组样例,一开始我没明白为什么这个样例是不对的。后来我发现进去肯定是按照123的顺序的,而且3在最上面,1在最下面,所以1是不可能比2先出来的(因为这里只有进去的一条通道,不能再退回到进入的通道中去,这里和栈不一样)

那么 123 213, 这组样例,是可以成功的,命令为“IN IN OUT OUT IN OUT”。

所以这个过程就是模拟每当在栈中进去一个数时,判断已经在栈中的数是不是和OUT命令中的数字相同,若相同,则出栈。

#include<stdio.h>
#include<string.h>
int main(){
	int n,top,i,j,k;
	char in[12],out[12],s[12],d[12];
	while(scanf("%d",&n)!=EOF){
		top=0;  k=j=0;
		//主要是看d[]的数组,若它为0,则表示的是out命令,反之是in命令。
		scanf("%s%s",in,out);
		for(i=0;i<n;i++){
			s[top++]=in[i];
			d[k++]=1;
			//这里是关键;
			while(top>0 && s[top-1]==out[j]) {
				top--; j++; d[k++]=0;
			}
		}
		//top表示是否已经全部出栈完毕;
		if(top==0){
			puts("Yes.");
			for(i=0;i<k;i++){
				printf("%s\n",d[i]==0 ? "out":"in");
			}
		}
		else puts("No.");
		puts("FINISH");
	}
}
时间: 2024-10-12 21:57:24

G - Train Problem I(模拟题)的相关文章

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 1023 Train Problem II 这题运用到大数相乘+大数相除+卡特兰数

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

hdu(5402)——Travelling Salesman Problem(模拟题)

啊...这道题我一开始的想法是dp,因为我们要求的是在这个区间中和的最大值. 但是没想到只要暴力就好了. 这道题用到了一个著名的想法是:黑白棋盘染色问题. 题意: 现在给你一个n*m的矩阵,然后告诉你每个矩阵中的数字,然后现在要从左上角走到右下角,然后问你所能获得的数字和的最大值是多少.当然,你只能往四个方向走,而且每个点只能走一次.并且叫你输出路径. 思路: 这里我分了三种情况. 1)首先当行或者列数都是1的时候,那么我们就只可能有一种走法(横着走或者是竖着走)然后获取所有的数值. 2)当行数

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

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

FZU Problem 2034 Password table (简单模拟题)

这种简单题做了好长时间,我是不是有点逗? 地址:http://acm.fzu.edu.cn/problem.php?pid=2034 不解释了,自己看吧,练手的好题 上个代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <stdio.h> #include <string.h> #include <stdlib.h>

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

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()

cdoj 25 点球大战(penalty) 模拟题

点球大战(penalty) Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/25 Description 在足球比赛中,有不少赛事,例如世界杯淘汰赛和欧洲冠军联赛淘汰赛中,当比赛双方经过正规比赛和加时赛之后仍然不分胜负时,需要进行点球大战来决定谁能够获得最终的胜利.点球大战的规则非常简单,两方轮流派出球员罚点球,每方各罚5个.当5轮点球结束以后如果仍然不分胜负,则进入一轮定胜