【PAT甲级】1051 Pop Sequence (25 分)(栈的模拟)

题意:

输入三个正整数M,N,K(<=1000),分别代表栈的容量,序列长度和输入序列的组数。接着输入K组出栈序列,输出是否可能以该序列的顺序出栈。数字1~N按照顺序随机入栈(入栈时机随机,未知在何时入栈,可能在某个栈内元素出栈以后)。

AAAAAccepted code:

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 int a[1007][1007];
 5 int main(){
 6     ios::sync_with_stdio(false);
 7     cin.tie(NULL);
 8     cout.tie(NULL);
 9     int m,n,k;
10     cin>>m>>n>>k;
11     for(int i=1;i<=k;++i){
12         for(int j=1;j<=n;++j)
13             cin>>a[i][j];
14         int flag=0;
15         stack<int>sk;
16         int top=1;
17         for(int j=1;j<=n;++j){
18             sk.push(j);
19             if(sk.size()>m&&!flag){
20                 cout<<"NO
";
21                 flag=1;
22             }
23             while(!sk.empty()&&sk.top()==a[i][top]){
24                 sk.pop();
25                 ++top;
26             }
27         }
28         if(!sk.empty()&&!flag)
29             cout<<"NO
";
30         else if(!flag)
31             cout<<"YES
";
32     }
33     return 0;
34 }
保持热爱 不懈努力 不试试看怎么知道会失败呢(划掉) 世上无难事 只要肯放弃(划掉)
原文地址:https://www.cnblogs.com/ldudxy/p/11619023.html