1. 程式人生 > >hdu 1022 簡單的棧應用

hdu 1022 簡單的棧應用

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 321 3 123 312

Sample Output

Yes. in in in out out out FINISH No. 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,指向棧頂)。

解題思路:首先我們得知道進站不一定是一次性進入,也就是說中途可以出站,不然簡直太容易了。然後就是簡單的棧模擬了。直接上程式碼。

程式碼:

#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;
}