【PAT甲级】1051 Pop Sequence (25 分)(栈的模拟)

时间:2022-10-30 12:06:49

题意:

输入三个正整数M,N,K(<=1000),分别代表栈的容量,序列长度和输入序列的组数。接着输入K组出栈序列,输出是否可能以该序列的顺序出栈。数字1~N按照顺序随机入栈(入栈时机随机,未知在何时入栈,可能在某个栈内元素出栈以后)。

AAAAAccepted code:

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[][];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int m,n,k;
cin>>m>>n>>k;
for(int i=;i<=k;++i){
for(int j=;j<=n;++j)
cin>>a[i][j];
int flag=;
stack<int>sk;
int top=;
for(int j=;j<=n;++j){
sk.push(j);
if(sk.size()>m&&!flag){
cout<<"NO\n";
flag=;
}
while(!sk.empty()&&sk.top()==a[i][top]){
sk.pop();
++top;
}
}
if(!sk.empty()&&!flag)
cout<<"NO\n";
else if(!flag)
cout<<"YES\n";
}
return ;
}