1. 程式人生 > >PTA 02-線性結構4 Pop Sequence (25 分)

PTA 02-線性結構4 Pop Sequence (25 分)

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

本題大意:給出若干組陣列,檢測每組陣列(每個數字池)能否通過指定大小的棧pop得到,入棧順序:1到N。

輸入第一行:M(棧的最大容量)N(要檢測的數字有多少個)K(有多少組要檢測的數字);後K行每行N個數字,代表要檢測的陣列;輸出:如果可以通過棧的pop得到則輸出YES,否則輸出NO

如: 5 6 4 3 7 2 1:push:1,2,3,4,5; pop:5, push:6, pop:6, pop:4, pop:3, push:7, pop:7, pop:2, pop:1,即可得到。

解題思路:設定兩個指標分別指向數字池的首端和棧頂,根據先入後出原則,用陣列模擬棧,棧頂應該是陣列的最後一個元素。如果棧頂元素小於當前數字池的那個元素,則向棧裡壓入一個新數字,如果棧頂元素等於當前數字池的那個數字,則出棧一個數組,同時數字池指標向後移一位,表示下一個檢測的元素,如果棧頂元素大於數字池指標指向的元素,則說明數字池的元素被放在了棧頂下面,無法彈出,則為NO;注意棧滿的情況,在入棧之前應該先判斷棧是否滿了。

c語言版:

#include <stdio.h>
#include <string.h>
int main() {
    int M, N, K;
    scanf("%d %d %d", &M, &N, &K);
    int stack[M], pool[N];
    while(K--) {
        //初始化要檢測的數字池
        int flag = 0;
        for (int i = 0; i < N; ++i) {
            scanf("%d", &pool[i]);
        }
        int pPool = 0, pStack = -1, num = 1;
        while (pPool < N) {
            if (pStack == -1) {
                // 棧空
                stack[++pStack] = num++;
            } else {
                // 棧不為空,檢查棧頂元素和數字池頂元素是否匹配
                if (stack[pStack] == pool[pPool]) {
                    // 匹配
                    pStack--;
                    pPool++;
                } else if (stack[pStack] < pool[pPool]) {
                    // 棧頂元素比較小,則先判斷後入棧
                    if ((++pStack) == M) {
                        flag = 1;
                        break;
                    }
                    stack[pStack] = num++;
                } else {
                    flag = 1;
                    break;
                }
            }
        }
        if (flag) printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}
/*
5 7 1
1 7 6 5 4 3 2

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
 */

c++版

#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int main() {
    int M, N, K;
    cin >> M >> N >> K;
    while(K--) {

        vector<int> pool(N, 0);
        // 輸入一個數字池
        for (int i = 0; i < N; ++i) {
            cin >> pool[i];
        }
        stack<int> stack1;
        int pPool = 0, num = 1, flag = 0;
        while (pPool < N) {
            if (stack1.empty()) {
                // 判斷是不是為空,如果是,則入棧
                stack1.push(num++);
            } else {
                // 不為空,看下棧頂元素和數字池的當前元素是不是一樣
                if (stack1.top() == pool[pPool]) {
                    // 一樣則出棧一個元素,數字池的指標指向下一個元素
                    stack1.pop();
                    pPool++;
                } else if (stack1.top() < pool[pPool]) {
                    // 棧頂元素比較小,則先檢測是不是滿棧,再入棧
                    if (stack1.size() == M) {
                        flag = 1;
                        break;
                    } else {
                        stack1.push(num++);
                    }
                } else {
                    // 棧頂元素比較大則不能產生數字池的陣列的順序
                    flag = 1;
                    break;
                }

            }
        }
        if (flag) cout << "NO" << endl;
        else cout << "YES" << endl;
    }
    return 0;
}
/*
5 7 5
1 7 6 5 4 3 2

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
 */