1051 Pop Sequence
题目大意
有一个容量限制为M的栈,按照1~n的顺序依次入栈,给出一系列出栈的顺序,判断是否可能
注意
1、主要是思路,从1~n依次入栈,如果当前栈顶等于当前出栈序列需要出栈的数,就需要出栈,直到不等于。
代码
#include<stdio.h>
#include<iostream>
#include<stack>
using namespace std;
stack<int> s;
int m,n,k;
int a[1001];
int main(void){
scanf("%d%d%d",&m,&n,&k);
for(int i=0;i<k;i++){
//初始化数据
int count=0;
bool flag=true;
//清空栈
while(!s.empty())s.pop();
//读入数据
for(int j=0;j<n;j++)scanf("%d",a+j);
for(int t=1;t<=n;t++){//1-n依次入栈
s.push(t);
if(s.size()>m){flag=false;break;}
//如果当前等待入栈的数不等于等待出栈的数
if(s.top()!=a[count])continue;
//如果当前等待入栈的数等于当前等待出栈的数
if(s.top()==a[count]){
//弹出栈顶数
while(s.empty()!=true&&s.top()==a[count]){
s.pop();
count++;
}
}
}
if(s.empty()&&flag==true)printf("YES\n");
else printf("NO\n");
}
}