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 which can keep MM numbers at most. Push NN numbers in the order of 1, 2, 3, ..., NN 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 MM is 5 and NN 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): MM (the maximum capacity of the stack), NN (the length of push sequence), and KK (the number of pop sequences to be checked). Then KK lines follow, each contains a pop sequence of NN 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
/*
评测结果
时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户
2017-07-08 15:55 答案正确 25 5-3 gcc 3 1
测试点结果
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 15/15 2 1
测试点2 答案正确 3/3 2 1
测试点3 答案正确 2/2 2 1
测试点4 答案正确 2/2 3 1
测试点5 答案正确 1/1 2 1
测试点6 答案正确 2/2 1 1 */
#include<stdio.h>
#define MAXLEN 1000 /*下面这段代码只能拿18/25分
int main()
{
int i,j,m,n,k,last,flag,temp;
scanf("%d %d %d",&m,&n,&k);
for (i=0;i<k;i++){
flag=1;
last=0;
for(j=0;j<n;j++){
scanf("%d",&temp);
if (temp-last>m) flag=0;
last=temp;
}
if(flag==0) printf("NO\n");
else printf("YES\n"); }
}
*/ struct stack{
int data[MAXLEN];
int top;
int max;
}; struct stack workstack; int GetTop(struct stack stc)
{
if(stc.top>=0) return stc.data[stc.top];
else return 0;
} int Push(struct stack *stc,int item)
{
if (stc->top==MAXLEN-1) return 0;
else {
stc->data[++(stc->top)]=item;
// printf("--PUSH %d,%d\n",stc->data[(stc->top)],stc->top);//test
return 1;
}
} int Pop(struct stack *stc)
{
if(stc->top<0) return 0;
else{
// printf("--POP %d,%d\n",stc->data[(stc->top)],stc->top);//test
return stc->data[(stc->top)--];
}
} int main()
{
int i,j,m,n,k,numforpush,errorflag,temp;
scanf("%d %d %d",&m,&n,&k);
workstack.max=m;
for (i=0;i<k;i++){
errorflag=0;
numforpush=1;
workstack.top=-1;
for(j=0;j<n;j++){
scanf("%d",&temp);
if (errorflag==1) continue;
// printf("--GET %d\n",temp);//test
while(workstack.top<workstack.max-1 && numforpush<=n && GetTop(workstack)!=temp){
// printf("--IN WHILE workstack.top=%d,max=%d,numforpush=%d\n",workstack.top,workstack.max,numforpush);//test
Push(&workstack,numforpush);
numforpush++;
} if(GetTop(workstack)==temp){
Pop(&workstack);
}
else{
errorflag=1;
} }
if(errorflag==1)
printf("NO");
else
printf("YES");
if(i!=k-1) putchar('\n');
} }
PTA 02-线性结构4 Pop Sequence (25分)的更多相关文章
- 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 ...
- pat02-线性结构4. Pop Sequence (25)
02-线性结构4. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given ...
- 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 1051 Pop Sequence (25 分)
返回 1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ...
- 线性结构4 Pop Sequence
02-线性结构4 Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the or ...
- 数据结构练习 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 ...
- 浙大数据结构课后习题 练习二 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 ...
- 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 p ...
- [刷题] PTA 02-线性结构4 Pop Sequence
模拟栈进出 方法一: 1 #include<stdio.h> 2 #define MAXSIZE 1000 3 4 typedef struct{ 5 int data[MAXSIZE]; ...
随机推荐
- 520 Detect Capital 检测大写字母
给定一个单词,你需要判断单词的大写使用是否正确.我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是大写,比如&q ...
- 475 Heaters 加热器
详见:https://leetcode.com/problems/heaters/description/ C++: class Solution { public: int findRadius(v ...
- [已读]编写高质量代码 改善JavaScript程序的188个建议
吐槽一万遍,买的最后悔的一本,没有之一,大量篇幅抄袭<高性能javascript>,我记得还有部分抄袭<javascript精粹>,<javascript模式>有没 ...
- spring boot druid mybatis多数据源
一.关闭数据源自动配置(很关键) @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) 如果不关闭会报异常:o ...
- chrome inspect出现白屏的解决方案
点inspect后 弹出框,可是里面一片白色 PS:原效果不是这样,只是图找不到随便p的 原因可以看这个:http://www.cnblogs.com/slmk/p/7591126.html 大概意思 ...
- Codeforces GYM 100741A . Queries
time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input output stan ...
- 【转载】SQL Server 2012 日志传送
SQL Server 2012 日志传送 一.准备: 数据库为完全恢复模式,并事先做一次完全备份. 共享一个文件夹,主机备份放在这个文件夹,而且客户机有权访问这个共享文件夹. 二.基本配置 1.启动配 ...
- 计数器:counter
组成:2属性,1方法 属性1: counter-reset 命名 属性2: counter-increment 启动/自增 方法 : counter()/counters() 调用方法 1.计数器 命 ...
- Web框架_MVC vs MVT
MVC 大部分开发语言中都有MVC框架 MVC框架的核心思想是:解耦 降低各功能模块之间的耦合性,方便变更,更容易重构代码,最大程度上实现代码的重用 M表示model,主要用于对数据库层的封装 V表示 ...
- java 随机数 <%=System.currentTimeMillis() %>
java 随机数<c:set var="version" value="<%=System.currentTimeMillis() %>"/& ...