1. 程式人生 > >1051. Pop Sequence (25)

1051. Pop Sequence (25)

例如 problem true height ces stdio.h capacity amp sel

題目例如以下:

Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.

Input Specification:

Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.

Output Specification:

For each pop sequence, print in one line "YES" if it is indeed a possible pop sequence of the stack, or "NO" if not.

Sample Input:
5 7 5
1 2 3 4 5 6 7
3 2 1 7 5 6 4
7 6 5 4 3 2 1
5 6 4 3 7 2 1
1 7 6 5 4 3 2
Sample Output:
YES
NO
NO
YES
NO


題目要求推斷一個長度為N的出棧序列能否夠用一個1~N的入棧序列和一個容量為M的堆棧完畢。

乍一看題目似乎非常難入手。但僅僅要細致思考一下,能夠發現當中有非常大的規律可循。

我們以3217564為例,一個容量為5的堆棧,由於入棧序列已經確定為1~7,依照以下的步驟思考:

我們設將要入棧的元素為push_index,要輸出的元素為x:

x=3,push_index=1。還沒有壓到3,要輸出三。必須一直壓到3。然後彈出3。此時push_index=4(下一次要壓入的是4)。堆棧如今從底到頂分別為12。

接下來x=2,push_index=4。已經無法通過壓入元素到達x。因此僅僅有棧頂為x時才可能得到要求的輸出序列,這時候推斷棧頂是不是x。不是則這個序列無法實現,是則繼續推斷下一個x。

相同地,x=1也是這樣處理,我們發現堆棧中有12,正好輸出為21,因此能夠正確輸出321。

以下x=7,push_index=4,一直壓入到x再彈出。則push_index=7,堆棧中在彈出7後為456。

以下x=5,push_index=7,這時候又要推斷棧頂是不是5了,發現棧頂是6,已經不可能得到要求的輸出序列。


通過這樣舉例,抽象例如以下:

①假設當前想要輸出的元素x<將要入棧的元素push_index(由入棧序列得到,從1開始),則一直壓入到x。然後彈出x,同一時候push_index指向下一個元素。

②假設當前想要輸出的元素x==將要入棧的元素push_index。說明壓入再彈出就可以。這時候直接把push_index後移繼續處理。

③假設當前想要輸出的元素x>將要入棧的元素push_index。說明僅僅能通過出棧方式得到x。看棧頂是否是x,是則彈出。繼續處理。否則輸出NO。

④僅僅有③的條件始終滿足,才輸出YES。

代碼例如以下:

#include <iostream>
#include <stack>
#include <vector>
#include <stdio.h>

using namespace std;

int main()
{
    stack<int> nums;
    int M,N,K;
    int push_index = 1; // 壓棧序列
    int index = 1; // 遍歷到的出棧序列位置
    int x; // 要找的元素
    bool right_flag = false;
    cin >> M >> N >> K;
    vector<int> pop_sequence(N);
    for(int i = 0; i < K; i++){
        for(int j = 0; j < N; j++){
            cin >> pop_sequence[j];
        }
        push_index = 1;
        index = 1;
        while(!nums.empty()) nums.pop();
        right_flag = true;
        while(index < N){
            x = pop_sequence[index - 1];
            index++;
            if(x < push_index){
                if(!nums.empty() && nums.top() == x){
                    nums.pop();
                }else{
                    right_flag = false;
                    break;
                }
            }else if(x == push_index){
                push_index++;
            }else{ // x < push_index。應該壓入到index為止,彈出這個,再向後推斷

                if(push_index > N) {
                    right_flag = false;
                    break;
                }

                for(; push_index <= x; push_index++){
                    nums.push(push_index);
                    if(nums.size() > M){
                        right_flag = false;
                        break;
                    }
                }
                nums.pop();
            }
        }
        if(right_flag) cout << "YES" << endl;
        else cout << "NO" << endl;
    }

    return 0;
}


1051. Pop Sequence (25)