• 1051. 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
    


    题目要求推断一个长度为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;
    }
    


  • 相关阅读:
    文本框小写变大写控制
    SQL2005 递归查询示例,非常方便
    GridView分页后进行添加,删除操作后,仍返回到当前页码
    从ASP.NET 1.1升级到ASP.NET 2.0要考虑的Cookie问题
    英语常用口语
    ASP.NET会话(Session)保存模式
    .NET2005文档自动生成
    JavaScript 弹出窗口总结
    SQL SERVER和SYBASE的渊源
    A versatile HDR Video Production System笔记
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7345307.html
Copyright © 2020-2023  润新知