数据结构练习 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 ...
 
随机推荐
- 集合(Collection,set,list,map)
			
package cn.hncu.col.col; import java.util.ArrayList;import java.util.Collection;import java.util.Has ...
 - MAC地址查询 Linux/Unix操作系统mac地址怎么查
			
Linux/Unix操作系统熟悉的人都通常是用console命令控制台来进行相应的操作. Linux/Unix操作系统查看网卡mac地址的方法可以通过以下命令获得: 1.ifconfig -a 2. ...
 - 记录一次centos升级gblic的教训
			
前些天公司项目需要对上传的图片打水印,前端采用angularjs,后端nodejs,网上一搜,找到了一个images的库,在本地Windows环境下试了下还不错,然后就上传测试服务器(centos6. ...
 - skynet是什么
			
云风的skynet,定义为一个游戏服务器框架,用c + lua基于Actor模型实现.代码极其精简,c部分的代码只有三千行左右. 整个skynet框架要解决的核心问题是:把一个消息 ...
 - 阿里云ECS被攻击
			
今天发现阿里云ECS被攻击了,记录一下, /1.1 Match1:{ :;};/usr/bin/perl -e 'print .content-type: text/plain.r.n.r.nxsuc ...
 - python tornado+mongodb的使用
			
tornado tar xvzf tornado-1.2.1.tar.gz cd tornado-1.2.1 python setup.py build sudo python setup.py in ...
 - 归并排序算法(C#实现)
			
归并排序(Merge Sort)是利用"归并"技术来进行排序.归并是指将若干个已排序的子文件合并成一个有序的文件.归并排序有两种方式:1): 自底向上的方法 2):自顶向下的方法 ...
 - mysql嵌套查询
			
select * from(select t.`name` `name`,count(*) count from company t group by t.`name`) aa where aa.co ...
 - iOS的Mantle实战分析
			
公司项目之前的model层代码是我使用JSON工具直接生成Objective-C代码的,当时还是觉得相当省事的,毕竟我经历过无model层的NSDictionary“黑暗”时期.但是随着项目的推进,问 ...
 - Codevs 1702 素数判定 2(Fermat定理)
			
1702 素数判定 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description 一个数,他是素数么? 设他为P满足(P< ...