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 Output:
YES
NO
NO
YES
NO
基本思路
先把输入的数列用数组保存,然后依次将1,2,3...N入栈,每次入栈判断栈顶是否与数组中当前要输出的元素相等,如果相等则出栈(循环)。如果遇到栈满或无法输出数组所有元素的情况则输出No。
代码
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
template < class T >
class AStack {
private:
int size; // 数组的规模
T * Array; // 存放堆栈元素的数组
int top; // 栈顶所在数组元素的下标
public:
AStack(int MaxStackSize) // 构造函数
{
size = MaxStackSize; Array = new T[MaxStackSize]; top = -1;
}
~AStack() { delete[] Array; } // 析构函数
bool Push(const T& item); // 向栈顶压入一个元素
bool Pop(T & item); // 从栈顶弹出一个元素
T Top() const { return Array[top]; } // 查看栈顶元素
int IsEmpty(void) const { return top == -1; }
// 检测栈是否为空
int IsFull(void) const { return top == size - 1; }
// 检测栈是否为满
void clear(void) { top == -1; } // 清空栈
};
template < class T >
bool AStack<T>::Push(const T& item)
{
if (this->IsFull())
{
// cout << "full" << endl;
return false;
}
else
{
Array[top + 1] = item;
top++;
}
return true;
}
template < class T >
bool AStack<T>::Pop(T& item)
{
if (this->IsEmpty())
{
//cout << "empty" << endl;
return false;
}
else
{
item = Array[top];
top--;
}
return true;
}
int Judge(int M, int N);
int main()
{
int M, N, K;
cin >> M;
cin >> N;
cin >> K;
int *yes = new int[K];
for (int i = 0; i < K; i++)
{
yes[i] = Judge(M, N);
}
for (int i = 0; i < K; i++)
{
if (yes[i])
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
int Judge(int M, int N)
{
int item[1000], now = 0;
AStack<int> stack(M);
for (int i = 0; i < N; i++)
{
cin >> item[i];
}
for (int i = 1; i <= N; i++)
{
stack.Push(i);
while (item[now] == stack.Top())
{
int temp;
stack.Pop(temp);
now++;
if (now == N)
{
//cout << "YES" << endl;
return 1;
break;
}
continue;
}
if (stack.IsFull())
{
//cout << "NO" << endl;
return 0;
break;
}
}
return 0;
}
总结
试了下,好像在读输入的时候同时输出也是可以的。
02-线性结构4 Pop Sequence的更多相关文章
- 线性结构4 Pop Sequence
02-线性结构4 Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the or ...
- pat02-线性结构4. Pop Sequence (25)
02-线性结构4. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given ...
- 02-线性结构4 Pop Sequence
02-线性结构4 Pop Sequence (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 https://pta.p ...
- 02-线性结构3 Pop Sequence
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...
- 数据结构练习 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 ...
- 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 ...
- 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 ...
- [刷题] PTA 02-线性结构4 Pop Sequence
模拟栈进出 方法一: 1 #include<stdio.h> 2 #define MAXSIZE 1000 3 4 typedef struct{ 5 int data[MAXSIZE]; ...
随机推荐
- PHP知识大全
--------------------------------------------------------- PHP知识大全 ---------------------------------- ...
- ThreadPoolExecutor系列<二、ThreadPoolExecutor 代码流程图>
本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7681648.html 1.ThreadPoolExecutor代码 ...
- 使用ftp软件上传下载php文件时换行丢失bug(全部变为一行)
文章来源:http://www.piaoyi.org/computer/ftp-php-r-n-bug.html 正 文: 在使用ftp软件上传下载php源文件时,我们偶尔会发现在本地windows下 ...
- Arduino.最小系统面包板搭建
最早试过用万用板做过最小系统,主要用来烧录芯片 后来为了方便,用面包板也搭了一个最小系统, 但不采用杜邦线,因为飞来飞去的线太乱了 因此就有了这个简洁的版本,先上个成品图 用个烧录器就可以很方便的烧写 ...
- jstl 中 <c:foreach> 多级循环
- LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...
- 用gdb调试python多线程代码-记一次死锁的发现
| 版权:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.如有问题,可以邮件:wangxu198709@gmail.com 前言 相信很多人都有 ...
- common lisp的宏的工作模式
(defmacro our-expander (name) ‘(get ,name ’expander))(defmacro our-defmacro (name parms &body bo ...
- 关于laravel5.5控制器方法参数依赖注入原理深度解析及问题修复
在laravel5.5中,可以根据控制器方法的参数类型,自动注入一个实例化对象,极大提升了编程的效率,但是相比较与Java的SpringMVC框架,功能还是有所欠缺,使用起来还是不太方便,主要体现在方 ...
- js返回到上一个页面刷新与不刷新的写法
返回上个页面刷新: <script>window.location.href=document.referer</script> 返回上个页面不刷新 <script> ...