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的更多相关文章

  1. 线性结构4 Pop Sequence

    02-线性结构4 Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the or ...

  2. pat02-线性结构4. Pop Sequence (25)

    02-线性结构4. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given ...

  3. 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 ...

  4. 数据结构练习 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. [刷题] PTA 02-线性结构4 Pop Sequence

    模拟栈进出 方法一: 1 #include<stdio.h> 2 #define MAXSIZE 1000 3 4 typedef struct{ 5 int data[MAXSIZE]; ...

随机推荐

  1. 使用 python 进行身份证号校验

    使用 python 代码进行身份证号校验 先说,还有很多可以优化的地方. 1.比如加入15位身份证号的校验,嗯哼,15位的好像没有校验,那就只能提取个出生年月日啥的了. 2.比如判断加入地址数据库,增 ...

  2. flask开启调试的四种模式

    flask开启调试的四种模式 在app.run()中加一个参数, 'debug=True'就可以开启debug模式 from flask import Flask app = Flask(__name ...

  3. MySQL5.7使用Notifier启动、停止服务时出现的问题

    1.选择右击右下角 MySQL Notifier ,选择 Actions -> Manage Monitored Items 2.选择当前的服务 MySQL57  并进行删除 3.然后点击  a ...

  4. 【Canvas】(1)---概述+简单示例

    Canvas---概述+简单示例 如果通俗的去理解Canvas,我们可以去理解成它类似于我们电脑自带的画图工具一样,canvas首先是选择一块画布,然后在这个画布上描绘我们想画的东西,画好后展示给用户 ...

  5. 《民国奇探》的弹幕有点逗比,用 Python 爬下来看看

    电视剧<民国奇探>是一部充斥着逗比风的探案剧,剧中主要角色:三土.四爷.白小姐,三土这个角色类似于<名侦探柯南>中的柯南但带有搞笑属性,四爷则类似于毛利小五郎但有大哥范且武功高 ...

  6. Python常见数据结构-Tuple元组

    Python Tuple基本特点 元组与列表类似,不同之处在于元组的元素不能修改. 与字符串和列表一样,可以根据下标进行切片索引. 元组使用小括号,单一元素的元组定义是必须加一个逗号. Python ...

  7. 学习Saleforce | 业内第一份Salesforce学习者数据报告

    自从自由侠部落这个Salesforce中文学习平台成立以来,我们接触到了越来越多的Salesforce的学习者,由衷感觉到这个学习生态圈愈发蓬勃发展. 为了了解Salesforce学习者的基本情况.现 ...

  8. 【JAVA】并发-基础IO

    一.java.io包支持.java的IO流有输入.输出两种,每种输入.输出流又可分为字节流.字符流两大类,字节流以字节为单位处理IO操作,字符流以字符为单位处理IO操作 JDK 1.4以后有java. ...

  9. 联通友华通信光纤猫PT952G设置无线路由光猫桥接拨号

    #0x1 登陆后台,点击网络,点击宽带设置.选择第二个接口. 0x2 只修改模式,改成Bridge,其他无需修改.然后直接接路由器拨号就行,或者电脑都行. 0x4  恢复默认拨号,这样修改以后,直接连 ...

  10. 彻底解决Python编码问题

    1. 基本概念 字符集(Character set) 解释:文字和符合的总称 常见字符集: Unicode字符集 ASCII字符集(Unicode子集) GB2312字符集 编码方法(Encoding ...