--- title: "弹出序列" created: 2025-11-28 tags: - 算法 --- # 弹出序列 ## 题目 [弹出序列](https://www.acwing.com/problem/content/1537/) ![[image-54d676a0.png]] ## 思路分析 见上一题 [[栈的压入、弹出序列|栈的压入、弹出序列]] 多了一个判断 如果长度大于给定容量 就false ## 代码实现 ```cpp #include using namespace std; const int NUM=1010; int cmp[NUM]; int N,M,K; bool check() { stack 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>cmp[i]; if(check()) puts("YES"); else puts("NO"); } return 0; } ``` ## 同类题型 ## 视频讲解 --- ⬅️ [[字符串|字符串]] 🏠 [[00-刷题理模型]] ➡️ [[括号匹配|括号匹配]]