【1051】Pop Sequence (25 分)

简介: 【1051】Pop Sequence (25 分)【1051】Pop Sequence (25 分)
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>  
#include<map>
#include<vector>
#include<queue> 
#include <stack>
using namespace std;  
const int maxn=1010;
int arr[maxn]; //保存题目给定的出栈序列
stack<int> st;  //定义栈st,用以存放int型元素
int main(){   
  int m,n,T;
  scanf("%d%d%d",&m,&n,&T);
  while(T--){   //循环执行T次
    while(!st.empty()) {  //清空栈
      st.pop();
    }
    for(int i=1;i<=n;i++){
      scanf("%d",&arr[i]);
    }
    int current=1; //指向出栈序列中的带出栈元素
    bool flag=true;
    for(int i=1;i<=n;i++){
      st.push(i); //把i压入栈
      if(st.size()>m){ //如果此时栈中元素个数大于容量m,则序列非法
        flag=false;
        break;
      }
      //栈顶元素与出栈序列当前位置的元素相同时
      while(!st.empty()&&st.top()==arr[current]){
        st.pop();//反复弹栈并令current++
        current++;
      }
    }
    if(st.empty()==true&&flag==true){
      printf("YES\n"); //栈空且flag==true时表明合法
    }else{
      printf("NO\n");
    }
  }
  return 0;   
}
相关文章
|
11月前
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
57 0
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero
|
人工智能 测试技术
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
72 0
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
|
存储
PAT (Advanced Level) Practice - 1051 Pop Sequence(25 分)
PAT (Advanced Level) Practice - 1051 Pop Sequence(25 分)
83 0
【1051】Pop Sequence (stack)
出栈是否合法要满足:(1)能出栈(2)不超过栈的限制容量。 思路:模拟,将1~n依次入栈,在入栈中——如果入栈的元素恰好等于出栈序列当前等待出栈的元素,就让栈顶元素出栈,同时把出栈序列当前等待出栈的元素位置标记后移1位。 ——举个栗子:出栈顺序为3 2 1 7 5 6 4时,1入
91 0
【1140】Look-and-say Sequence (20分)
【1140】Look-and-say Sequence (20分) 【1140】Look-and-say Sequence (20分)
63 0
【1085】Perfect Sequence (25 分)
【1085】Perfect Sequence (25 分) 【1085】Perfect Sequence (25 分)
74 0
【1121】Damn Single (25分)【set】
【1121】Damn Single (25分)【set】 【1121】Damn Single (25分)【set】
78 0