数据结构练习 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 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
- #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;
- int i, j;
- int input, temp;
- bool flag = true;
- stack<int> sta;
- for (i = ; i < K; i++)
- {
- temp = ;
- flag = true;
- for (j = ; j < N; j++)
- {
- cin >> input;
- while (sta.size() <= M && flag)
- {
- if (sta.empty() || sta.top() != input)
- {
- sta.push(temp++);
- }
- 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 ;
- }
数据结构练习 02-线性结构3. Pop Sequence (25)的更多相关文章
- pat02-线性结构4. Pop Sequence (25)
02-线性结构4. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given ...
- 线性结构4 Pop Sequence
02-线性结构4 Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the or ...
- 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 ...
- 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 ...
- 浙大数据结构课后习题 练习二 7-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 ...
- 02-线性结构4 Pop Sequence
02-线性结构4 Pop Sequence (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 https://pta.p ...
- Java数据结构介绍(线性结构和非线性结构)
数据结构包括:线性结构和非线性结构. 线性结构 数据元素之间存在一对一的线性关系 包括顺序存储结构和链式存储结构.顺序存储的线性表称为顺序表,顺序表中的存储元素是连续的 链式存储的线性表称为链表,链表 ...
- PAT 解题报告 1051. Pop Sequence (25)
1051. Pop Sequence (25) Given a stack which can keep M numbers at most. Push N numbers in the order ...
- PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)
1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ord ...
随机推荐
- 系统调用表 linux 2.6.32
[root@localhost log]# find / |grep syscall_table /usr/src/kernels/linux-/arch/x86/kernel/syscall_tab ...
- iOS开发UI篇-tableView在编辑状态下的批量操作(多选)
先看下效果图 直接上代码 #import "MyController.h" @interface MyController () { UIButton *button; } @pr ...
- 移动终端学习1:css3 Media Queries简介
移动终端学习之1:css3 Media Queries简介 1.简介 这篇文章写的不错,我就不重复了,来个链接:http://www.w3cplus.com/content/css3-media-qu ...
- 深入理解计算机系统第二版习题解答CSAPP 2.9
基于三元色R(红)G(绿)B(蓝)关闭(0)和打开(1),能够创建8种不同的颜色,如下: R G B 颜色 R G B 颜色 0 0 0 黑色 1 0 0 红色 0 0 1 蓝色 1 0 1 红紫色 ...
- mono & apache install
1.red hat 6安装完后网卡是默认不启动的 作为双生兄弟的CENTOS同样如是 第一步 设置网卡开机启动 进入 路径此目录下修改网卡配置文件 如果网卡驱动正常 会有如下文件 只要修改 ifcfg ...
- 《转》手把手教你使用Git
Git是分布式版本控制系统,那么它就没有中央服务器的,每个人的电脑就是一个完整的版本库,这样,工作的时候就不 需要联网了,因为版本都是在自己的电脑上.既然每个人的电脑都有一个完整的版本库,那多个人如何 ...
- 推荐几个对Asp.Net开发者比较实用的工具 2
推荐几个对Asp.Net开发者比较实用的工具.大家有相关工具也可以在评论区留言,一起努力学习. 作为程序员要有挑战精神,大家可以尝试一下这些工具. 已经有篇文章写到了vs的扩展工具,这里不再累赘,请查 ...
- MySQL基本查询语句
创建一张表 create table user ( id ) not null, name ) not null, birthDate date not null, gender ) not null ...
- C#垃圾回收机制详解
一.托管代码/非托管代码 C#代码通过C#编译器编译成程序集,程序集由微软中间语言组成,CLR会为程序集开辟一个应用程序域,程序集就是运行在这个应用程序域里面的,应用程序域是相互独立的,互不影响. 托 ...
- Js 学习资料
Js 语法 http://www.php100.com/manual/jquery/ jqGrid控件 http://www.trirand.com/blog/jqgrid/jqgrid.html