hdu 1022 简单的栈应用

来源:互联网 发布:淘宝新店卖啥好 编辑:程序博客网 时间:2024/06/05 17:48

本题链接:点击打开链接                         

Train Problem I

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


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 3213 123 312
 

Sample Output
Yes.inininoutoutoutFINISHNo.FINISH
Hint
Hint
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.".
 

Author
Ignatius.L
 

Recommend
We have carefully selected several similar problems for you:  1026 1023 1032 2063 1015 
 

 

题意:火车的进出站问题,先给出N个火车,再按序列一的方式进站,判断能否以序列二的方式出站,若能先输出“Yes.”,再输出出站步骤,以FINISH结束,若不能,输出“No.”,仍以FINISH结束。

 

PS:对栈的理解不深有错误请大佬指正,然后在网上查找了一些栈的知识。

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)(里面还有链表和队列的知识,大家也可以看看)。

 

解题思路:首先我们得知道进站不一定是一次性进入,也就是说中途可以出站,不然简直太容易了。然后就是简单的栈模拟了。直接上代码。

 

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <stack>using namespace std;int main(){    char s1[100],s2[100];                         //进站序列为s1,目标出站序列为s2.    int n,j,k,f[100];                            //f数组用来记录进出站(栈)    stack<char>s;                               //栈的建立    while(~scanf("%d%s%s",&n,s1,s2))    {        while(!s.empty())                     //测试数据有多组,若栈不为空就先清空栈,防止意外发生            s.pop();        memset(f,-1,sizeof(f));        j=k=0;        for(int i=0; i<n; i++)        {            s.push(s1[i]);                            //将s1[i]元素压入队列,即为进栈            f[k++]=1;                                //f[x]=1 表示进栈            while(!s.empty()&&s.top()==s2[j])       //若栈顶元素(火车)与待出站火车相同则出站            {                f[k++]=0;                            //f[x]=0表示出栈                s.pop();                            //删除出栈元素                j++;                                //下一个出站火车就绪            }        }        if(j==n)                                   //若出站火车等于待出站的火车,出站成功        {            printf("Yes.\n");            for(int i=0; i<k; i++)              //输出火车进出站步骤            {                if(f[i])                    printf("in\n");                else                    printf("out\n");            }        }        else                                    //否则表示出站失败            printf("No.\n");        printf("FINISH\n");    }    return 0;}



 

1 0
原创粉丝点击