hdoj 1022Train Problem I

/*Train Problem I

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

Total Submission(s) : 6   Accepted Submission(s) : 2

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*/

<span style="font-size:18px;">#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
	stack<int>s;
	int n;
	char a[9],b[9];
	while(~scanf("%d %s %s",&n,&a,&b))
	{
		int i,j,k=0,l=0,p=2,c[20];
		s.push(a[0]);
		c[1]=1;
	   while(l<n)
	  {
		  if(s.empty())
		  {
			if(l<(n-1))
			{
				l++;
				s.push(a[l]);
				c[p]=1;
				p++;
			}
			else
			{
				printf("Yes.\n");
				for(i=1;i<=(2*n);i++)
				{
					if(c[i]>0)
						printf("in\n");
					else
						printf("out\n");
				}
			    printf("FINISH\n");
				break;
			}
		  }
		  while(!s.empty())
		  {
			if(s.top()!=b[k])
			{
				l++;
				if(l==n)
				{
					while(!s.empty())
					{
						s.pop();
					}
					printf("No.\n");
                    printf("FINISH\n");
					break;
				}
				s.push(a[l]);
				c[p]=1;
				p++;
			}
			if(s.top()==b[k])
			{
				c[p]=0;
				p++;
				s.pop();
				k++;
			}
		  }
		 }
	}
	return 0;
}
 </span>

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-13 19:54:06

hdoj 1022Train Problem I的相关文章

杭电1022Train Problem I

地址:http://acm.hdu.edu.cn/showproblem.php?pid=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 i

HDOJ 1001_Sum Problem

AC代码: #include <iostream> #include <cstdio> using namespace std; int main(void) { freopen("in.txt","r",stdin); long long n; while(scanf("%lld",&n)!=EOF) { if(n%2==0) printf("%lld\n\n",n/2*(n+1)); els

【HDOJ】3509 Buge&#39;s Fibonacci Number Problem

快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. 1 /* 3509 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #incl

【HDOJ 1002】A + B Problem II

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 238517    Accepted Submission(s): 45969 Problem Description I have a very simple problem for you. Given two integers A and B, you

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]

hdoj 4971 A simple brute force problem. 【最大闭合权 --&gt; 最小割】

题目:hdoj 4971 A simple brute force problem. 题意:给出 n 个任务和 m 项技术,完成某个任务需要其中几项技术,完成某个任务有奖金,学习某个技术需要钱,技术之间有父子关系,某项技术可能需要先学习其他技术,然后问你选择做那些任务获得收益最大? 分析:看题意的黑体字部分,就是一个标准的闭合权问题,这个题目的关键忽悠点在于技术之间的关系,导致很多人想到了dp以及树形dp. 其实就是一个闭合权问题模板,官方题解说如果技术之间存在相互的关系需要缩点,其实不用缩点也

HDOJ 4974 A simple water problem

A simple water problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 173    Accepted Submission(s): 112 Problem Description Dragon is watching competitions on TV. Every competition is held be

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 1002 A + B Problem II (Big Numbers Addition)

题目链接在此?http://acm.hdu.edu.cn/showproblem.php?pid=1002 这题也比较简单,只需要开三个长度为1000的char数组来分别储存a.b.ans,再利用我们加法的算法,先向右对齐再相加.注意一下进位时的特殊情况就好了. 不过笔者的代码写好后提交上去,两次Presentation Error,然后才发现只是最后多输出一个空行的问题  =.= Orz /**  * HDOJ 1002 A + B Problem II  * Big Numbers Addi