hpu 1022&&nyoj 150 Train Problem I 【栈】

10 篇文章 0 订阅

Train Problem I

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


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

题意:

输入火车的个数n,编号(1~n),然后火车站就像栈一样,只能先进后出,然后给入站顺序,和出站顺序

问是否能完成这样的进出方式,如果能,需要输出进出的提示和顺序....


分析:

这个题就是个运用栈的工作原理,解决匹配问题,完全模拟火车进出站...


思路:

两个状态存下来,然后按火车进站的顺序,先入栈一个元素,循环判断,如果栈内元素和末状态相同,那就出栈(栈指针移动到前一个元素,相当于火车出站),否则就进栈(将进站的火车放进栈里).....一直循环到两个状态有一个结束.....

其实就是完全模拟进站出站的全过程,关键是循环控制指针!!!


#include<stdio.h>
#include<string.h>
int main()
{
	int n,i,j,k,p;//i,j,k 三个指针,对应三个数组... 
	char x[15],y[15],z[15];//两个状态储存,数组z 模拟栈 
	int s[105];//记录进栈或出栈
	while(~scanf("%d",&n))
	{
		scanf("%s%s",x,y);
		k=p=0;z[0]=x[0];
		for(i=1,j=0,j=0;j<n||i<n;++i)
		{
			if(y[j]==z[k])//对应的相同,就出栈
			{
				s[p++]=0;//记录出栈
				--i;--k;++j;//三个指针都要移动哦~
			}
			else//对应的不同,也就是不能匹配....
			{
				s[p++]=1;//继续记录...
				++k;//移动栈的指针,继续下一次匹配
				z[k]=x[i];//继续入栈...
			}
		}
		if(k>=0)//栈不为空,证明两个状态没有完全匹配的情况
		{
			printf("No.\n");
		}
		else//匹配成功
		{
			printf("Yes.\n");
			printf("in\n");//输出这个是因为我的思路是要提前进栈一次
			for(i=0;i<p;++i)//下面就是按照记录的输出啦~~
			{
				if(s[i]==1)//进栈...
				{
					printf("in\n");
				}
				else//出栈...
				{
					printf("out\n");
				}
			}
		}
		printf("FINISH\n");//结束...
	}
	return 0;
}




#include<stdio.h>//2015年9月9日,0:12
#include<string.h>
char x[1005],y[1005],stack[1005];
int jl[1005],n;
void slove()
{
	memset(jl,0,sizeof(jl));
	memset(stack,0,sizeof(stack));
	int i,j,k,kase;
	i=j=k=kase=0;
	while(i<n)
	{
		if(y[j]!=stack[i])
		{
			stack[k++]=x[i++];
			jl[kase++]=1;
			//--k;++i;++j;
		}
		while(j<n&&y[j]==stack[k-1])
		{
			++j;--k;
			jl[kase++]=0;
		}
	}
	if(k!=0)
	{
		printf("No.\n");
	}
	else
	{
		printf("Yes.\n");
		for(i=0;i<kase;++i)
		{
			if(jl[i])
			{
				printf("in\n");
			}
			else
			{
				printf("out\n");
			}
		}
	}
	printf("FINISH\n");
}

int main()
{
	while(~scanf("%d",&n))
	{
		scanf("%s%s",x,y);
		slove();
	}
	return 0;
}  



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值