hdoj 1022 Train Problem I

Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24980    Accepted Submission(s):
9424

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

此题并不像题中测试数据一样好像是逆序输出即可,而是要用栈的思维,并不是说必须所有的车都进站了  才可以统一按顺序出来,而是说可以先进入一辆车然后这辆车出来,然后再进下一辆车,也可以先进两辆然后出来一辆再进一辆,即何时进站何时出站都可以(前提是符合题目的进出站要求即先进后出)

STL代码:

#include<stdio.h>
#include<string.h>
#include<stack>
#define MAX 1100
using namespace std;
int main()
{
	int n,m,j,i,t,k;
	char jin[MAX],chu[MAX];//进展顺序和出站顺序数组
	int vis[MAX];//记录第i次是进站还是出站
	while(scanf("%d",&t)!=EOF)
	{
		scanf("%s %s",jin,chu);
		stack<char>s;//定义一个字符型的栈
		memset(vis,0,sizeof(vis));
		for(i=0,j=0,k=0;i<t;i++)
		{
			s.push(jin[i]);
			vis[k++]=1;
		    while(!s.empty()&&s.top()==chu[j])//如果站不为空且栈顶的元素与出站顺序的元素相同
		    {
		    	j++;//删除已出的元素
		    	vis[k++]=0;//出站并标记
		    	s.pop();//删除栈首元素
		    }
		}
		if(s.empty())
		{
			printf("Yes.\n");
			for(i=0;i<k;i++)
			{
				if(vis[i])
				printf("in\n");
				else
				printf("out\n");
			}
		}
		else
		printf("No.\n");
		printf("FINISH\n");
	}
	return 0;
}

 用数组实现:

#include<stdio.h>
#include<string.h>
#define MAX 1100
char zhan[MAX];
char jin[MAX],chu[MAX];//进展顺序和出站顺序数组
int vis[MAX];//记录第i次是进站还是出站
int main()
{
	int n,m,j,i,s,sum,t,top,k;
	while(scanf("%d",&t)!=EOF)
	{
		scanf("%s %s",jin,chu);
		memset(vis,0,sizeof(vis));
		top=0;k=0;
		for(i=0,j=0,k=0;i<t;i++)
		{
			zhan[top++]=jin[i];//存栈
			vis[j++]=1;
			while(top>=0&&zhan[top-1]==chu[k])//如果站不为空且栈顶的元素与出站顺序的元素相同
			{
				vis[j++]=0;//出站并标记
				top--;//删除栈首元素
				k++;//删除已出的元素
			}
		}
		if(top<0)
		{
			printf("Yes.\n");
			for(i=0;i<j-1;i++)
			{
				if(vis[i])
				printf("in\n");
				else
				printf("out\n");
			}
		}
		else
		printf("No.\n");
		printf("FINISH\n");
	}
	return 0;
}

  

时间: 2024-11-07 17:09:59

hdoj 1022 Train Problem I的相关文章

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

hdoj 1022 Train Problem I 【顺序栈】

最近打算重新看一边数据结构,昨天看的时候感觉栈这部分部分能看懂了. 于是就赶紧来实践一下!! Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19493    Accepted Submission(s): 7305 Problem Description As the new term comes, t

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]

H 1022 Train Problem Ⅰ

题意:给我们两个序列,看能否通过压栈,出栈将第一个序列转换成第二个. 思路:将序列 1 依次压栈,同时看是否和序列 2 当前元素相同 代码如下: #include<iostream> #include<stack> #define max 100 using namespace std; int main() { stack<char>s; int n,i,j,k,result[max]; char str1[max],str2[max]; while(cin>&

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 (数据结构 —— 栈)

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

HDU 1022 Train Problem I(栈的操作规则)

传送门:http://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): 45375    Accepted Submission(s): 16966 Problem Description As the new term come

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

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