弹出序列

题目 弹出序列

image-54d676a0

思路分析

见上一题 栈的压入、弹出序列

多了一个判断 如果长度大于给定容量 就false

代码实现

#include<bits/stdc++.h>

using namespace std;

const int NUM=1010;

int cmp[NUM];

int N,M,K;

bool check()

{

    stack<int> st;

    for(int i=1,j=0;i<=N;i++){

        st.push(i);

        if(st.size()>M)

            return false;

        while(st.size()&&st.top()==cmp[j]){

            st.pop();

            j++;

        }

    }

    return st.empty();

}

int main()

{

    cin>>M>>N>>K;

    while(K--){

        for(int i=0;i<N;i++)

            cin>>cmp[i];

        if(check())

            puts("YES");

        else

            puts("NO");

    }

    return 0;

}

同类题型

视频讲解


⬅️ 字符串 🏠 00-刷题理模型 ➡️ 括号匹配