[刷题] PTA 02-线性结构4 Pop Sequence
模拟栈进出
方法一:
1 #include<stdio.h>
2 #define MAXSIZE 1000
3
4 typedef struct{
5 int data[MAXSIZE];
6 int top;
7 }SqStack;
8
9 int InitStack(SqStack *s){
10 s->top=-1;
11 return 0;
12 }
13
14 int Push(SqStack *s,int e){
15 if(s->top==MAXSIZE) return 1;
16 s->top++;
17 s->data[s->top]=e;
18 return 0;
19 }
20
21 int Pop(SqStack *s){
22 int e;
23 if(s->top==-1) return 1;
24 e = s->data[s->top];
25 s->top--;
26 return e;
27 }
28
29 int main(){
30 int m,n,k,i,h;
31 SqStack s,t;
32 scanf("%d%d%d",&m,&n,&k);
33 while(k--){
34 InitStack(&s);
35 InitStack(&t);
36 for(i=n-1;i>=0;i--){
37 scanf("%d",&(t.data[i]));
38 }
39 t.top=n-1;
40 i=0;
41 while(i<=n){
42 if(s.data[s.top]==t.data[t.top]&&s.top!=-1){
43 Pop(&s);
44 Pop(&t);
45 }else if(s.top<m-1 && i<n){
46 i++;
47 Push(&s,i);
48 }else{
49 break;
50 }
51 }
52 if(s.top==-1 && t.top==-1){
53 printf("YES\n");
54 }else{
55 printf("NO\n");
56 }
57 }
58 return 0;
59 }
分析:
1、自定义栈
2、通过两个栈模拟进出
方法二:
1 #include<stdio.h>
2 #include<iostream>
3 #include<vector>
4 #include<stack>
5 using namespace std;
6
7 int M,N,K;
8
9 int Check(vector<int> &v) {
10 int i=0;
11 int num=1;
12 int cap=M+1;
13 stack<int> sta;
14 sta.push(0);
15 while(i<N) {
16 while(v[i]>sta.top()&&sta.size()<cap)
17 sta.push(num++);
18 if(v[i++]==sta.top())
19 sta.pop();
20 else
21 return 0;
22 }
23 return 1;
24 }
25
26 int main() {
27 vector<int> vec(N,0);
28 scanf("%d%d%d",&M,&N,&K);
29 for(int i=0; i<K; i++) {
30 for(int j=0; j<N; j++) {
31 int number;
32 scanf("%d",&number);
33 vec.push_back(number);
34 }
35 if(Check(vec)) printf("YES\n");
36 else printf("NO\n");
37 vec.clear();
38 }
39 return 0;
40 }
分析:
1、利用c++容器vector和stack模拟
[刷题] PTA 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 ...
- 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 ...
- [刷题] PTA 02-线性结构3 Reversing Linked List
链表逆序 1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 using namesp ...
- [刷题] PTA 02-线性结构1 两个有序链表序列的合并
程序: 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef int ElementType; 5 typedef st ...
- 02-线性结构4 Pop Sequence
02-线性结构4 Pop Sequence (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 https://pta.p ...
- 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 ...
随机推荐
- SpringBoot-08 SpringSecurity
SpringBoot-08 SpringSecurity 创建了一个新项目,创建时选择导入starter-web 1.环境搭建 1.1 导入thymeleaf <dependency> & ...
- [模拟]P1047 校门外的树
校门外的树 题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,- ...
- spring5源码编译过程中必经的坑
spring源码编译流程:Spring5 源码下载 第 一 步 : https://github.com/spring-projects/spring-framework/archive/v5.0.2 ...
- xman_2019_format(非栈上格式化字符串仅一次利用的爆破)
xman_2019_format(非栈上格式化字符串仅一次利用的爆破) 首先检查一下程序的保护机制 然后用IDA分析一下 存在后门 首先malloc了一片堆空间,读入数据 把刚刚读入的数据当作格式化字 ...
- Chrome扩展开发基础教程(附HelloWorld)
1 概述 Chrome扩展开发的基础教程,代码基于原生JS+H5,教程内容基于谷歌扩展开发官方文档. 2 环境 Chrome 88.0.4324.96 Chromium 87.0.4280.141 B ...
- Day01_10_Scanner 接收用户输入
Scanner函数 import java.util.Scanner; class ScannerTest { public static void main(String[] args){ Syst ...
- rabbitmq五种模式详解(含实现代码)
一.五种模式详解 1.简单模式(Queue模式) 当生产端发送消息到交换机,交换机根据消息属性发送到队列,消费者监听绑定队列实现消息的接收和消费逻辑编写.简单模式下,强调的一个队列queue只被一个消 ...
- 手把手教你搭建自己的Angular组件库 - DevUI
摘要:DevUI 是一款面向企业中后台产品的开源前端解决方案,它倡导沉浸.灵活.至简的设计价值观,提倡设计者为真实的需求服务,为多数人的设计,拒绝哗众取宠.取悦眼球的设计.如果你正在开发 ToB 的工 ...
- 2021软工-CSDN APP分析
项目 内容 这个作业属于哪个课程 2021春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 案例分析作业要求 我在这个课程的目标是 提升软件开发能力,提高团队协作能力 这个作业在哪个具体方面 ...
- 1028 List Sorting
Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...