题目描述

  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.

输入格式

  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 sequence 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.

输出格式

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

输入样例
5 7 3
1 2 3 4 5 6 7
3 2 1 7 5 6 4
5 6 4 3 7 2 1 输出样例
YES
NO
YES 

题意

  有一个容量限制为M的栈,先分别把1,2,3,n依次入栈,并给出一系列出栈顺序,问这些出栈顺序是否可能?

思路

  按照题目的要求进行模拟,将1~n依次入栈,在入栈的过程中如果入栈的元素恰好等于出栈序列当前等待出栈的元素,那么就让栈顶元素出栈,同时把出栈序列当前等待出栈的元素位置标记后移1位。此时只要栈顶元素依然等于出栈序列当前等待出栈的元素,则继续出栈。

代码

 1 #include<iostream>
2 #include<stack>
3 using namespace std;
4 const int maxn = 1010;
5 int arr[maxn]; //保存题目给的出栈序列
6 stack<int> st; //定义栈st,用以存放int型元素
7 int main()
8 {
9 int m, n, T;
10 cin>>m>>n>>T;
11 while(T--){ //循环T次
12 while(!st.empty()) //清空栈
13 st.pop();
14 for(int i=1;i<=n;i++) //读入数据
15 cin>>arr[i];
16 int current = 1; //指向出栈序列中的待出栈元素
17 bool flag = true;
18 for(int i=1;i<=n;i++){
19 st.push(i); //把i压入栈
20 if(st.size()>m){ //如果此时栈中元素个数大于容量m,则序列非法
21 flag=false;
22 break;
23 }
24 //栈顶元素与出栈序列当前位置的元素相同时
25 while(!st.empty()&&st.top()==arr[current]){
26 st.pop(); //反复弹栈并令current++
27 current++;
28 }
29 }
30 if(st.empty()==true&&flag==true){
31 cout<<"TES"<<endl;
32 }
33 else{
34 cout<<"NO"<<endl;
35 }
36 }
37 return 0;
38 }

Pop Sequeue的更多相关文章

  1. 数组中pop()和reverse()方法调用

    数组的倒序排列,可以采用reverse()和pop()方法进行排列.

  2. js中push(),pop(),unshift(),shift()的用法小结

    1.push().pop()和unshift().shift() 这两组同为对数组的操作,并且会改变数组的本身的长度及内容. 不同的是 push().pop() 是从数组的尾部进行增减,unshift ...

  3. 1051. Pop Sequence

    原题连接:https://www.patest.cn/contests/pat-a-practise/1051 题目: Given a stack which can keep M numbers a ...

  4. .pop ----remove 删除

    s = {1,2,3,4,5,6,'sn','7'} s.pop()#删除随机值 print(s)#{2, 3, 4, 5, 6, '7', 'sn'} s.remove('sn')#删除值 prin ...

  5. git stash和git stash pop

    git stash 可用来暂存当前正在进行的工作, 比如想pull 最新代码, 又不想加新commit, 或者另外一种情况,为了fix 一个紧急的bug,  先stash, 使返回到自己上一个comm ...

  6. 实现一个 能在O(1)时间复杂度 完成 Push、Pop、Min操作的 栈

    一,问题描述 实现一个栈(元素遵守先入后出顺序),能够通过 min 方法在 O(1)时间内获取栈中的最小元素.同时,栈的基本操作:入栈(Push).出栈(Pop),也是在O(1)时间内完成的. 二,问 ...

  7. Javascript的shift()和push(),unshift()和pop()方法简介

    栈方法: Javascript为数组专门提供了push()和pop()方法,以便实现类似栈的行为.来看下面的例子: var colors=new Array();       //创建一个数组 var ...

  8. JS数组操作示意图(shift,unshift,pop,push)

    shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift(); //a:[2,3,4,5] b: ...

  9. UINavigationController导航控制器初始化 导航控制器栈的push和pop跳转理解

    (1)导航控制器初始化的时候一般都有一个根视图控制器,导航控制器相当于一个栈,里面装的是视图控制器,最先进去的在最下面,最后进去的在最上面.在最上面的那个视图控制器的视图就是这个导航控制器对外展示的界 ...

随机推荐

  1. Hive实战UDF 外部依赖文件找不到的问题

    目录 关于外部依赖文件找不到的问题 为什么要使用外部依赖 为什么idea 里面可以运行上线之后不行 依赖文件直接打包在jar 包里面不香吗 学会独立思考并且解决问题 继承DbSearcher 读取文件 ...

  2. View的简单说明

    一)三个结构体:CGPoint.CGSize.CGRect 1. CGPoint struct CGPoint { CGFloat x; CGFloat y; }; typedef struct CG ...

  3. [BUUCTF]REVERSE——[HDCTF2019]Maze

    [HDCTF2019]Maze 附件 步骤: 例行检查,32位程序,upx壳 upx脱壳儿后扔进32位ida,首先检索程序里的字符串 有类似迷宫的字符串,下面也有有关flag的提示字符串,但是没法进行 ...

  4. 小迪安全 Web安全 基础入门 - 第三天 - 抓包&封包&协议&APP&小程序&PC应用&WEB应用

    一.抓包工具 1.Fiddler.Fiddler是一个用于HTTP调试的代理服务器应用程序,能捕获HTTP和HTTPS流量,并将其记录下来供用户查看.它通过使用自签名证书实现中间人攻击来进行日志记录. ...

  5. LuoguP5139 z小f的函数 题解

    Content 给定 \(T\) 个二次函数 \(y=ax^2+bx+c\),有若干次操作,有一个操作编号 \(p\),保证仅为以下这五种: 操作 \(1\):给定 \(k\),将函数图像向上移动 \ ...

  6. Python sys模块 os模块、OS.open() | open() | OS._exit() | sys.exit() | exit()

    sys模块:负责程序和Python交互. sys常用方法:===========================  sys.stdout.write('please:')val = sys.stdin ...

  7. git 修改东西之后提交命令

    1.git add * 添加东西 2.git status 查看要提交的东西 3.git commit -m "已经修改LogController文件" 提交 4.pwd 看当前目 ...

  8. Json解析案例-teachers数据集

    背景: 通过平台执行接口时,接口往往返回的JSON串,所以平台要能提供方便快捷的JSON解析函数. 一.Json字符串: 1 { 2 "lemon": { 3 "teac ...

  9. ubuntu(Linux) c++ 获取本机IPv4和ipv6、查询本机IPv4,IPv6

    1.关于 演示环境: Linux xxxxxxx 5.4.0-47-generic #51-Ubuntu SMP Fri Sep 4 19:50:52 UTC 2020 x86_64 x86_64 x ...

  10. 【LeetCode】396. Rotate Function 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rotate-fu ...