Pop Sequence (栈)
Pop Sequence (栈)
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
一个数要出栈,那么比这个数小的数一定要先进栈
设 tem 为进栈的数。如果 栈为空 或 出栈的数 不等于 栈顶数,那么 tem进栈 后 自增。如果 出栈的数 等于 栈顶数,则出栈。如果栈溢出,那就错误(可能是要出栈的数太大 或者是 要出栈的数 小于 此刻的栈顶元素 )
#include <iostream>
#include <string>
#include<vector>
#include <stack>
using namespace std;
int main()
{
int i,j,m,k,n,x;
while(cin>>m)
{
cin>>n>>k;
for(i=0;i<k;i++)
{
bool is=true;
stack<int> tt;
int tem=1;
for(j=0;j<n;j++)
{
cin>>x;
while(tt.size()<=m && is)
{
if(tt.empty() || tt.top()!=x)
tt.push(tem++);
else if(tt.top()==x)
{
tt.pop();
break;
}
}
if(tt.size() > m)
{
is=false;
}
}
if(is) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
return 0;
}
Pop Sequence (栈)的更多相关文章
- PAT 1051 Pop Sequence[栈][难]
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 ...
- PAT Advanced 1051 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 ...
- 1051. Pop Sequence
原题连接:https://www.patest.cn/contests/pat-a-practise/1051 题目: Given a stack which can keep M numbers a ...
- 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 ...
- Pop Sequence
题目来源:PTA02-线性结构3 Pop Sequence (25分) Question:Given a stack which can keep M numbers at most. Push ...
- 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 ...
- 1051. Pop Sequence (25)
题目如下: Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N ...
- PAT1051:Pop Sequence
1051. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a ...
随机推荐
- iOS-实现验证码倒计时功能(1)
验证码倒计时按钮的应用是非常普遍的,该Blog就和你一起来写一个IDCountDownButton来实现验证码倒计时的效果.你可以想使用普通的UIButton类型按钮一样,只需要设置其倒计时时长(若未 ...
- [Oracle AR]Territory Flexfield
You can use the Territory Flexfield for recording and customized reporting on your territory informa ...
- IIS 发布网站到外网
前段时间做了一个项目在局域网中测试后要发布到外网上,一时间不知怎么搞,以为直接在IIS中修改发布时的IP就可以了,但是不可行,经过摸索终于成功发布到外网,下面是具体步骤. 前期准备:公网IP,掩码,网 ...
- MyBatis(3.2.3) - One-to-many mapping
In the sample domain model, a tutor can teach one or more courses. This means that there is a one-to ...
- ASP.NET播客(留言时间,投票IP,留言限字数,上传视频)
留言发布时间功能: 界面: 前台代码: 在Datalist控件中: 在<%#getIsDate(Convert.ToString(Eval("issuanceDate"))) ...
- android中Json数据保存方式
package com.example.savejsonproject; import java.io.File; import java.io.FileNotFoundException; impo ...
- 动态执行C#代码
using System; using System.CodeDom.Compiler;using System.Collections.Generic;using System.Linq;using ...
- 【整理修订】Android.mk详解
Android.mk详解 1. Android.mk 的应用范围 Android.mk文件是GNU Makefile的一小部分,它用来对Android程序进行编译. 一个Android.mk文件可以编 ...
- UIView-4-EventForViews(在view上加入button时候的事件处理)
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- datatable dateset 载体传递数据、存储过程
第一部分:数据库通过存储过程读取数据,通过datatable接受,前台通过asp:repeater.DataSource()和binding()绑定数据 /// <summary> /// ...