#include<iostream>
#include<stack>
using namespace std;
const int maxn = 1010;
int arr[maxn];
stack<int> st;
int main() {
int m, n, t;
scanf("%d%d%d", &m, &n, &t);
while(t--) {
while(!st.empty()) //清空
st.pop();
for(int i = 1; i <= n; i++)
scanf("%d", &arr[i]);
int cur = 1;
bool flag = true;
for(int i = 1; i <= n; i++) {//入栈一个元素,检查栈顶与出栈序列中当前元素是否匹配
st.push(i);
if(st.size() > m) {
flag = false;
break;
}
while(!st.empty() && st.top() == arr[cur]) {//匹配-弹出
st.pop();
cur++;
}
}
if(st.empty() == true && flag == true) {
printf("YES\n");
} else {
printf("NO\n");
}
}
system("pause");
return 0;
}
代码源自:https://blog.csdn.net/hy971216/article/details/81564172?utm_source=blogxgwz5