出栈序列的合法性判断
- 如果栈无限大:出栈序列中的每个数后面的比它小的数,是按递减排列的。
- 如果栈大小有限制:模拟
基础实验3-2.4 出栈序列的合法性 (25point(s))
代码:
#include <bits/stdc++.h>
using namespace std;
int const N = 1000 + 10;
stack<int>st;
int a[N];
int main(){
int m,n,k;
scanf("%d%d%d",&m,&n,&k);
while(k--){
while(!st.empty()) st.pop();
int pos = 1;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++){
if(st.size() != m) st.push(i);
while(!st.empty() && st.top() == a[pos]){
st.pop();
pos++;
}
}
if(st.empty()) printf("YES\n");
else printf("NO\n");
}
return 0;
}