【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;   
}
目录
打赏
0
0
0
0
14
分享
相关文章
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
98 0
PAT (Advanced Level) Practice - 1051 Pop Sequence(25 分)
PAT (Advanced Level) Practice - 1051 Pop Sequence(25 分)
117 0
【1140】Look-and-say Sequence (20分)
【1140】Look-and-say Sequence (20分) 【1140】Look-and-say Sequence (20分)
99 0
【1051】Pop Sequence (stack)
出栈是否合法要满足:(1)能出栈(2)不超过栈的限制容量。 思路:模拟,将1~n依次入栈,在入栈中——如果入栈的元素恰好等于出栈序列当前等待出栈的元素,就让栈顶元素出栈,同时把出栈序列当前等待出栈的元素位置标记后移1位。 ——举个栗子:出栈顺序为3 2 1 7 5 6 4时,1入
154 0
【1085】Perfect Sequence (25 分)
【1085】Perfect Sequence (25 分) 【1085】Perfect Sequence (25 分)
108 0
【1053】Path of Equal Weight (30 分)
【1053】Path of Equal Weight (30 分) 【1053】Path of Equal Weight (30 分)
125 0
【1060】Are They Equal (25分)
【1060】Are They Equal (25分) 【1060】Are They Equal (25分)
107 0
【1063】Set Similarity (25 分)
【1063】Set Similarity (25 分) 【1063】Set Similarity (25 分)
112 0
【1145】Hashing - Average Search Time (25分)【hash 平方探测法】
【1145】Hashing - Average Search Time (25分)【hash 平方探测法】 【1145】Hashing - Average Search Time (25分)【hash 平方探测法】
125 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等