HDU1022 Train Problem I(栈的应用)

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


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

Hint

Hint
        
 

 

题意:给出火车的入站顺序以及出站顺序,问能否由入站顺序得到出站顺序,如果能则输入每一步的操作(每节车厢进站还是出站)

解题思路:在每节车厢进站时判断是否能出站,如果能则出站。也就是如果当前车厢等于出站序列的第一个元素则能出站。

 

引用:https://blog.csdn.net/ld_1090815922/article/details/70215328

1. 定义

        栈(Stack),是硬件。主要作用表现为一种数据结构,是只能在某一端插入和删除的特殊线性表。它按照后进先出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数据被第一个读出来)。

  栈是允许在同一端进行插入和删除操作的特殊线性表。允许进行插入和删除操作的一端称为栈顶(top),另一端为栈底(bottom);栈底固定,而栈顶浮动;栈中元素个数为零时称为空栈。插入一般称为进栈(PUSH),删除则称为退栈(POP)。 栈也称为先进后出表。

 

2. 栈的方法

empty()  测试堆栈是否为空。返回boolean。

peek()     查看堆栈顶部的对象,但不从堆栈中移除它。返回泛型E。

pop()        移除堆栈顶部的对象,并作为此函数的值返回该对象。返回泛型E。

push(E item)   把项压入堆栈顶部。返回泛型E。

search(Object o)   返回对象在堆栈中的位置,以 1 为基数。返回int。

 

3. 栈的实现

        1、进栈(PUSH)算法

  ①若TOP≥n时,则给出溢出信息,作出错处理(进栈前首先检查栈是否

         已满,满则溢出;不满则作②);

  ②置TOP=TOP+1(栈指针加1,指向进栈地址);

  ③S(TOP)=X,结束(X为新进栈的元素);

 

  2、退栈(POP)算法

  ①若TOP≤0,则给出下溢信息,作出错处理(退栈前先检查是否已为空栈, 

         空则下溢;不空则作②);

  ②X=S(TOP),(退栈后的元素赋给X):

  ③TOP=TOP-1,结束(栈指针减1,指向栈顶)。

以上相关知识来自(http://www.360doc.com/content/12/1009/16/9400799_240455229.shtml)(里面还有链表和队列的知识,大家也可以看看)。

 

AC代码:

#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;

stack<char> s;
int n,op[110];//1表示进0表示出 
char in[110],out[110];

int main()
{
	while(~scanf("%d%s%s",&n,in,out))
	{
		while(!s.empty())
		{
			s.pop();
		}
		memset(op,-1,sizeof(op));
		
		int j=0,cnt=0;
		for(int i=0;i<n;i++)
		{
			s.push(in[i]);
			op[cnt++]=1;
			while(!s.empty()&&s.top()==out[j])
			{
				j++;
				s.pop();
				op[cnt++]=0;
			}
		}
		if(j==n)//全部出站 
		{
			printf("Yes.\n");
			for(int i=0;i<cnt;i++)
			{
				if(op[i]==1) printf("in\n");
				else printf("out\n");
			}
		}
		else printf("No.\n");
		printf("FINISH\n");
	}
	return 0;
}

一段时间没写博客了

不知道是键盘坏了还是博客编辑器的问题,老是误输入好多东西。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值