02-线性结构4 Pop Sequence
02-线性结构4 Pop Sequence (25分)
- 时间限制:400ms
- 内存限制:64MB
- 代码长度限制:16kB
- 判题程序:系统默认
- 作者:陈越
- 单位:浙江大学
https://pta.patest.cn/pta/test/3512/exam/4/question/62615
Given a stack which can keep MMM numbers at most. Push NNN numbers in the order of 1, 2, 3, ..., NNN 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 MMM is 5 and NNN 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): MMM (the maximum capacity of the stack), NNN (the length of push sequence), and KKK (the number of pop sequences to be checked). Then KKK lines follow, each contains a pop sequence of NNN 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
用回溯法(dfs)判断能否构造出目标序列
#include<bits/stdc++.h>
using namespace std; int gode[];
int M,N,K,num;
stack<int> Sta; bool dfs(int ipush,int ipop)//ipush,ipop为已完成的操作(push,pop)数
{
if(ipop==N)//成功构造gode
return true;
if(ipush<N&&Sta.size()<M)
{
Sta.push(num++);
if(dfs(ipush+,ipop)) return true;
num--;
Sta.pop();
}
if(ipop<ipush&&!Sta.empty()&&Sta.top()==gode[ipop])//栈顶元素即为gode的下一个数时才执行pop
{
int t=Sta.top();
Sta.pop();
if(dfs(ipush,ipop+)) return true;
Sta.push(t);
}
return false;
}
int main()
{
cin>>M>>N>>K;
for(int i=;i<K;i++)
{
num=;
for(int j=;j<N;j++)
cin>>gode[j];
puts(dfs(,)?"YES":"NO");
}
return ;
}
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-线性结构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 ...
- 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 ...
- [刷题] PTA 02-线性结构4 Pop Sequence
模拟栈进出 方法一: 1 #include<stdio.h> 2 #define MAXSIZE 1000 3 4 typedef struct{ 5 int data[MAXSIZE]; ...
随机推荐
- python--匿名函数、文件操作
一.匿名函数 语法: sum = lambda arg1, arg2: arg1 + arg2 #调用sum函数 print "Value of total : ", sum( 1 ...
- 好玩Python——PIL项目实训
PIL学习总结: 1. 2,PIL库概述: pil库可以完成图像归档和图像处理两方面功能的需求: 图像归档:对图像进行批处理,生成图像预览,图像转换格式等: 图像处理:图像基本处理,像素处理,颜色处理 ...
- SQL表的简单操作
创建数据库表,进行增删改查是我们操作数据库的最基础的操作,很简单,熟悉的请关闭,免得让费时间. 1.创建表: sql中创建数值类型字段要根据该字段值的增长情况选择类型: tinyint 占1个字节,长 ...
- 中阶d03.1 JDBCDemo
1. jdbc使用查看驱动的doc文档<connector-j.html> 2.代码实现:1. 注册驱动---2. 建立连接---3. 创建statement ,跟数据库打交道--- -- ...
- 二、Centos7—U盘启动盘制作
1,准备一个8gU盘. 2.iso系统镜像文件. 3.下载UltraISO软件制作启动盘. 4.运行UltraISO软件 5.在UltraISO软件中打开刚下载的Centos系统安装文件 6.开始 ...
- 加锁的位置 (eq:map<key,map<>> 双集合 怎么 只加锁 在用到的对象位置,而不是把整个集合锁住)
比如上边的map里套map 定义变量为data,例如组队副本 npc 为1 下有众多房间 即Map<1,<roomId,room>> ,处于多线程下,一个线程在 npc为1的下 ...
- ASP.NET Core中配置监听URLs的五种方式
原文: 5 ways to set the URLs for an ASP.NET Core app 作者: Andrew Lock 译者: Lamond Lu 默认情况下,ASP. NET Core ...
- Python-selenium安装与Java-selenium安装
一.Python安装及selenium的安装 1.安装Pythonhttps://www.Python.org2.安装setuptools.distribute.piphttps://pypi.pyt ...
- 在学习java之余,js的使用精髓-闭包和原型链
这里分享下廖雪峰官网写的js教程,内容写的比较实用,易懂,其中简介的原型链和闭包的知识,小伙伴们一起上呀,畅游在知识的海洋中: 地址:https://www.liaoxuefeng.com/wiki/ ...
- CodeForces - 876B H - 差异的可分割性
现在有n个整数,在这n个数中找出k个数,保证这k个数中任意两个数差的绝对值可以被m整除. Input第一行输入三个整数n,k,m(2<=k<=n<=100000,1<=m< ...