pat A1051
参考代码:
#include<iostream>
#include<stack>
using namespace std;
const int maxn=1111;
int n,m,k;
/*
如果数字相同则出栈
最后是根据栈是否为空,来决定是输出yes或者no
*/
int a[maxn];
stack<int> q;
int main()
{
cin >> n >> m >> k;
int temp1;
for(int i=0; i<k; i++)
{
while(!q.empty()) //每次需要清空栈,方便下次使用
q.pop();
for(int j=0; j<m; j++) //对数列a输入数字
{
cin >> a[j];
}
int ans=0;
for(int k=1; k<=m; k++) //将1--m完全输入到栈中
{
q.push(k);
if(q.size()>n) //如果栈的长度完全超过n,则退出
break;
while(!q.empty() && q.top()==a[ans]) //如果当前队首元素完全等于此时数组a对应的元素,则q.pop,然后ans向后移动
{
q.pop();
ans++;
}
}
if(q.size()==0)
cout << "YES" << endl;
else
{
cout << "NO" << endl;
}
}
return 0;
}