Matter:
关键在于对栈的模拟。想想我们人是怎么模拟的,然后用机器语言模拟。记得最后的时候栈必须得是空的。
Code:
#include<iostream>
#include<stack>
using namespace std;
int main(){
int M , N , K ;
stack<int> s;// 这个放到外面来,手动清空一下
cin >> M >> N >> K;
for(int i = 1 ; i <= K ; i ++){
//手动清空栈
while(s.empty() == false){
s.pop();
}
// Step1:保存数值
int d[1005] , index = 1;
bool flag = true;
for(int j = 1 ; j <= N ; j ++){
scanf("%d" , &d[j]);
}
// Step2:管理数据
for(int j = 1 ; j <= N ; j ++){
s.push(j);
//如果超过了规定大小,错误
if(s.size() > M){
flag = false;
break;
}
//当前元素的数值和当前位置的数值相同
while(s.empty() == false && s.top() == d[index]) {
s.pop();
index ++;
}
}
//Step3: stack is empty and flag is true
if(flag == true && s.empty() == true){
cout << "YES" << endl;
}
else{
cout << "NO" << endl;
}
}
return 0;
}