02-线性结构4 Pop Sequence(25 分)

0人阅读 评论(0) 收藏 举报
分类:

题目来源:中国大学MOOC-陈越、何钦铭-数据结构-2018春
作者: 陈越
单位: 浙江大学

问题描述:
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

解答:模拟栈的运行,把1-N依次放入栈中,模拟,看能不能顺利全部弹出栈。

#include <iostream>
#include <stack>
using namespace std;

stack<int> moni;
int main()
{
    int MaxSize,N,testN;
    cin>>MaxSize>>N>>testN;
    int test[1001];
    for(int j=1; j<=testN; j++)
    {
        while(!moni.empty())
        {
            moni.pop();
        }
        for(int i=1; i<=N; i++)
            cin>>test[i];
        int i=1,temp=1;
        bool flag=true;
        while(i<=N)
        {
            moni.push(i);
            if(moni.size()>MaxSize)
            {
                flag=false;
                break;
            }
            while(!moni.empty()&&test[temp]==moni.top())
            {
                moni.pop();
                temp++;
            }
            i++;
        }
        if(!moni.empty())
            flag=false;
        if(flag)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}

查看评论

02-线性结构4 Pop Sequence (25分)

Given a stack which can keep MMM numbers at most. Push NNN numbers in the order of 1, 2, 3, …, NNN a...
  • wanmeiwushang
  • wanmeiwushang
  • 2016-09-30 20:29:18
  • 950

02-线性结构4. Pop Sequence(25)

题目的意思: 栈的大小M,输入序列的长度N(默认序列即为1,2,3…,N),入栈出栈的顺序不定。有K个测试序列,判断每一个测试序列是否是可能的出栈顺序。 核心:从出栈顺序推测入栈顺序关键点:当看到...
  • whzyb1991
  • whzyb1991
  • 2015-06-27 20:40:31
  • 3454

02-线性结构4. Pop Sequence (25)

原题链接:http://www.patest.cn/contests/mooc-ds/02-%E7%BA%BF%E6%80%A7%E7%BB%93%E6%9E%844 02-线性结构4. P...
  • ice_camel
  • ice_camel
  • 2015-04-22 15:24:07
  • 1488

02-线性结构3 Pop Sequence (25分)

02-线性结构3 Pop Sequence   (25分) Given a stack which can keep MM numbers at most. Push NN numb...
  • sysusyf
  • sysusyf
  • 2016-03-26 23:03:47
  • 272

(2016秋数据结构课后练习题总结)02-线性结构4 Pop Sequence (25分)

Given a stack which can keep MMM numbers at most. Push NNN numbers in the order of 1, 2, 3, ...,...
  • jiaomenglei
  • jiaomenglei
  • 2016-09-10 21:20:10
  • 825

PAT 02-线性结构3. Pop Sequence (25)

http://www.patest.cn/contests/mooc-ds2015spring/02-%E7%BA%BF%E6%80%A7%E7%BB%93%E6%9E%843 Given a st...
  • qq_19672579
  • qq_19672579
  • 2015-03-25 21:30:08
  • 951

02-线性结构4 Pop Sequence

题意有一个栈大小为M的栈,有一个数列1,2,3,…,n要入栈,你可以任意时候出栈,给你一个k个出栈的序列,判断是否可以由这个数列得到,如果可以输出YES,不能则输出NO。解题思路对于入栈出栈的队列我们...
  • codeforcer
  • codeforcer
  • 2016-10-10 18:14:40
  • 746

[PTA]02-线性结构4 Pop Sequence

Given a stack which can keep MM numbers at most. Push NN numbers in the order of 1, 2, 3, ..., N...
  • the_victory
  • the_victory
  • 2016-10-09 10:26:52
  • 1456

九度OJ-1442:A sequence of numbers

一道大数据处理的题目。因为数据过大故要注意两点:取模+二分求幂。   二分求幂就不细说了。取模用到了如下两个公式:   ①(a*b)%c=((a%c)*(b%c))%c   ②(a+b)%c=(...
  • u012963208
  • u012963208
  • 2017-02-06 15:18:18
  • 538

浙大PAT 1051题 1051. Pop Sequence

设置一个变量tmp,由1开始自增。当栈顶元素与输入的元素不匹配时将当前tmp入栈并tmp自加,当匹配时栈顶出栈。 #include #include using namespace std; int...
  • Sup_Heaven
  • Sup_Heaven
  • 2014-02-26 21:22:10
  • 1864
    个人资料
    持之以恒
    等级:
    访问量: 619
    积分: 270
    排名: 29万+
    文章存档