PAT甲级1051 【Pop Sequence】 (25)

这篇博客主要探讨了PAT甲级题目1051【Pop Sequence】的解决方案,作者采用了栈作为数据结构进行模拟。通过详细解释和展示C++代码,阐述了解题过程和思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

栈模拟,我用的是比较笨的方法,直接贴代码

#include<iostream>
#include<cstdio>
#include<stack>
#include<cstring>
using namespace std;
const int maxn = 1e3+10;
bool vis[maxn];

int main(){
    //freopen("in.txt", "r", stdin);
    int m, n, k, num, top;
    cin >> m >> n >> k;
    for(int i = 0; i<k; ++i){
        bool flag = true;
        stack<int> s;
        top = 0;
        memset(vis, false, sizeof vis);
        for(int j = 0; j<n; ++j){
            cin >> num;
            if(!flag) continue;
            if(num > top){
                for(int i = top+1; i<=num; ++i){
                    if(!vis[i]){
                        s.push(i);
                        if(i != num) top = i; //栈顶为第一个小于num且没有vis的元素 
                    }
                    if(s.size() > m) flag = false;
                }
                s.pop();
                vis[num] = true;
            }
            else if(num < top)  flag = false;
            else {
                s.pop();
                vis[top] = true;
                for(; top>=0; top--){
                    if(!vis[top]) break; //重新获取栈顶 
                }
            }
        }
        if(flag) cout << "YES\n";
        else cout << "NO\n";
    }

    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值