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.要pop n,前提是要push 1 2 3 … n-1。 num为既定序列1 2 3 … 例如,input为 4 3 2 1的4时,首先要push 1 2 3 才能push 4 pop 4 出栈。

2.stack.size() > M(the maximum capacity of the stack) 超出栈容量不可能。

思路:当栈顶元素不是input 则push(num++)直到input,随后pop input;或者stack.size() > M(the maximum capacity of the stack) 检测出不可能。

 #include<iostream>
#include<stack>
using namespace std; int main()
{
int M; //maximum capacity of the stack
int N; //the length of push sequence
int K; //the number of pop sequence to be checked
cin >> M >> N >> K;
bool flag = true;
int input, num; //num= 1.2.3.4.5... input为依次输入的检测序列值
stack<int> sta; for(int i = ; i < K; i++) {
num = ;
flag = true;
for(int j = ; j < N; j++) {
cin >> input;
while( sta.size() <= M && num ) {
if(sta.empty() || sta.top() != input) {
sta.push(num ++);
}else if(sta.top() == input) {
sta.pop();
break;
}
}
if(sta.size() > M ) //超出栈容量
flag = false;
}
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl; while(!sta.empty()) //清空栈
sta.pop();
}
return ;
}

参考:http://www.2cto.com/kf/201311/254409.html  开始误入了比大小的误区,这个比较细致易理解。记一笔。

02-线性结构3 Pop Sequence的更多相关文章

  1. 线性结构4 Pop Sequence

    02-线性结构4 Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the or ...

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

    02-线性结构4. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given ...

  3. 02-线性结构4 Pop Sequence

    02-线性结构4 Pop Sequence   (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 https://pta.p ...

  4. 数据结构练习 02-线性结构3. 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 p ...

  5. 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 p ...

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

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/665 5-3 Pop Sequence   (25分) Given a stack wh ...

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

  8. 02-线性结构4 Pop Sequence

    题目 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 ...

  9. [刷题] PTA 02-线性结构4 Pop Sequence

    模拟栈进出 方法一: 1 #include<stdio.h> 2 #define MAXSIZE 1000 3 4 typedef struct{ 5 int data[MAXSIZE]; ...

随机推荐

  1. JQuery基础教程:事件(下)

     事件传播     为了说明不可单击的页面元素处理单击事件的能力,例如样式转换器中包含按钮的div元素或者兄弟元素h3,我们来实现一个鼠标指针进入元素和离开元素时的效果,首先需要添加一种翻转状态,表明 ...

  2. 1、程序启动原理和UIApplication【转】

    一.UIApplication 1.简单介绍 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就代表一个应用程序. (2)每一个应用都有自己的UIApplica ...

  3. Windows 2008下部署Exchange Server 2007

    对于很多政府及企业来说,微软的邮件服务器Exchange Server都是一个不错的通信和协作平台选择,尤其新版邮件服务器Exchange Server 2007 和OCS的组合,在微软UC平台上创下 ...

  4. HIVE配置文件

    进入HIVE_HOME/conf 编辑文件hive-site.xml,内容如下:(这是伪分布式模式) 主要声明了以下几个内容: 数据仓库地址 数据库连接地址 数据库连接驱动 数据库连接用户名 数据库连 ...

  5. [WinForm]平均切割图片AvgCutImage

    昨天晚上下班前有朋友问我有的人的QQ空间相册的那种多个图片拼接成一张完整的图片的是怎么做到的比如像这样效果: 嘛,反正我是1000%不会使用这样封面来做网络相册的封面,因为用户体验实在是太差了.完全不 ...

  6. 【翻译】使用CSS3和jQuery制作跟随鼠标方位的Hover特效

    今天我们来学习如何通过CSS3的特性和jQuery来创建一个感知鼠标滑动方向的hover效果.当鼠标滑进的时候,遮罩层会从上次鼠标滑出的方向滑入,当鼠标滑出的时候,遮罩层会跟随鼠标,从鼠标滑出的方向滑 ...

  7. EXTJS 6 新特性(译文)

    Extjs 新特性 简介 使用extjs,sencha 团队开发一个简单的框架,可以为创建在任何类型设备上运行的应用,从手机端到平板电脑再到桌面应用,你将能够产生最佳的用户体验,编写更少的代码量,结合 ...

  8. 洛谷P1518 两只塔姆沃斯牛 The Tamworth Two

    P1518 两只塔姆沃斯牛 The Tamworth Two 109通过 184提交 题目提供者该用户不存在 标签USACO 难度普及+/提高 提交  讨论  题解 最新讨论 求数据 题目背景 题目描 ...

  9. 华为OJ平台——放苹果(典型整数划分问题)

    题目描述: 输入m,n,分别表示苹果数与盘子的总数,要求输出苹果放在n个盘子的方法总数(注意511和151是一种情况),例如输入 7 3 输出8((7),(6,1),(5,2),(4,3),(5,1, ...

  10. php json中文处理方法,请json更懂中文

    1.php5.3版本及以下.的处理方式 /** *php5.3版本以前,json中文问题的解决解决方案 */ function encode_json($str) { return urldecode ...