PAT (Advanced Level) Practice - 1051 Pop Sequence(25 分)

简介: PAT (Advanced Level) Practice - 1051 Pop Sequence(25 分)

题目链接:点击打开链接


题目大意:有个容量限制为 m 的栈,分别把1,2,3,...,n入栈,给出一个系列出栈顺序,问这些出栈顺序是否可能。


解题思路:用 vector 存储每次待检测的序列,用 stack 从1-n依次存储,每次存储时,先判断是否超过容量,以及模拟出栈顺序匹配比较。



AC 代码

#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;
int main()
{
    int cap,n,k;
    while(~scanf("%d%d%d",&cap,&n,&k))
    {
        while(k--)
        {
            vector<int> v(n+1);
            for(int i=1;i<=n;i++) scanf("%d",&v[i]);
            stack<int> sk;
            int f=1,j=1;
            for(int i=1;i<=n;i++)
            {
                sk.push(i);
                if(sk.size()>cap){f=0; break;}
                while(!sk.empty() && sk.top()==v[j])
                {
                    sk.pop();
                    j++;
                }
            }
            if(sk.size()!=0) f=0;
            puts(f?"YES":"NO");
        }
    }
    return 0;
}
目录
相关文章
PAT (Advanced Level) Practice - 1057 Stack(30 分)
PAT (Advanced Level) Practice - 1057 Stack(30 分)
108 0
PAT (Advanced Level) Practice - 1057 Stack(30 分)
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
117 0
|
存储
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
138 0
|
C++
PAT (Advanced Level) Practice - 1038 Recover the Smallest Number(30 分)
PAT (Advanced Level) Practice - 1038 Recover the Smallest Number(30 分)
124 0
PAT (Advanced Level) Practice - 1127 ZigZagging on a Tree(30 分)
PAT (Advanced Level) Practice - 1127 ZigZagging on a Tree(30 分)
122 0
|
存储 测试技术
PAT (Advanced Level) Practice - 1131 Subway Map(30 分)
PAT (Advanced Level) Practice - 1131 Subway Map(30 分)
112 0
PAT (Advanced Level) Practice - 1060 Are They Equal(25 分)
PAT (Advanced Level) Practice - 1060 Are They Equal(25 分)
102 0
PAT (Advanced Level) Practice - 1111 Online Map(30 分)
PAT (Advanced Level) Practice - 1111 Online Map(30 分)
114 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
130 0
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
119 0