7-3 Pop Sequence (25 分)

题目链接

栈模拟

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int N = 1005;
int out[N];

int main(){
	int m, n, k;
	scanf("%d%d%d", &m, &n, &k);
	while(k--){
		for(int i = 1; i <= n; i++){
			scanf("%d", &out[i]);
		}
		stack<int> st;
		int u = 1;
		bool flag = false;
		for(int v = 1; v <= n; v++){
			st.push(v); //每次加入数字 
			if(st.size() > m) {
				flag = true;
				break;
			}
			//栈顶相同  
			while(st.size() && st.top() == out[u]){
				st.pop();
				u++; 
			}
		}
		if(!flag && st.empty()) cout<<"YES"<<endl;
		else cout<<"NO"<<endl; 
	}
	
	
	return 0;
}

你可能感兴趣的:(数据结构与算法题目集,栈与队列)