Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.
Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.
For each pop sequence, print in one line "YES" if it is indeed a possible pop sequence of the stack, or "NO" if not.
5 7 5
1 2 3 4 5 6 7
3 2 1 7 5 6 4
7 6 5 4 3 2 1
5 6 4 3 7 2 1
1 7 6 5 4 3 2
YES
NO
NO
YES
NO
题目要求判断给出的数组是否符合出栈能形成的顺序。
所以我们肯定需要定义一个栈:
当给出数组当前的数字与栈顶的数字不相同且栈的大小符合题目要求时,就继续入栈;相同时出栈,空栈时出栈。最后判断数组里的所有数字是否都能被栈输出即可,判断代码如下:
- bool is_right(vector<int> a){
- stack<int> order;//栈每次使用过后要清空,所以干脆定义在这里
- int in_number[N];
- for(int i=0;i
//1-N的数字 - in_number[i]=i+1;
- }
- int index = 0;
- order.push(in_number[index++]);//先将第一个数字入栈
- int i=0;
- for(;i
size();){ - if(a[i]!=order.top()&&order.size()
//当不相同且栈容量够时入栈 - order.push(in_number[index]);
- index++;
- }
- else if(a[i] == order.top()){//相同时出栈,并继续向后判断
- i++;
- order.pop();
- }
- else{
- break;
- }
- if(order.empty()) order.push(in_number[index++]);//栈为空时入栈
- }
- if(i
size()) return false; - return true;
- }
1. 空栈的时候记得入栈,可以避免一进一出的情况下出错;
2. 每次使用完栈记得清空!!!!!
- #include
- using namespace std;
- int M;//栈的最大容量
- int N;//数组长度
- int K;//需要检查的数组数量
-
- bool is_right(vector<int> a){
- stack<int> order;//栈每次使用过后要清空,所以干脆定义在这里
- int in_number[N];
- for(int i=0;i
//1-N的数字 - in_number[i]=i+1;
- }
- int index = 0;
- order.push(in_number[index++]);//先将第一个数字入栈
- int i=0;
- for(;i
size();){ - if(a[i]!=order.top()&&order.size()
//当不相同且栈容量够时入栈 - order.push(in_number[index]);
- index++;
- }
- else if(a[i] == order.top()){//相同时出栈,并继续向后判断
- i++;
- order.pop();
- }
- else{
- break;
- }
- if(order.empty()) order.push(in_number[index++]);//栈为空时入栈
- }
- if(i
size()) return false; - return true;
- }
-
- int main(){
- cin>>M>>N>>K;
- vector<int> number[K];
- for(int i=0;i
- for(int j=0;j
- int t;
- cin>>t;
- number[i].push_back(t);
- }
- if(is_right(number[i])) cout<<"YES"<
- else cout<<"NO"<
- }
- return 0;
- }
-
相关阅读:
Java环形链表_leetcode141(快慢指针、set)
JavaWeb--JDBC核心技术
Qt 不同字符类型转换
Leetcode—1726.同积元组【中等】
Redisson
开发小程序遇到的问题
java优秀毕业生推荐系统ssm
基于idea的YonBuilder Premium开发者工具(原NCC开发插件)使用说明
2024滴滴校招面试真题汇总及其讲解(二)
Android 内容提供者和内容观察者:数据共享和实时更新的完美组合
-
原文地址:https://blog.csdn.net/weixin_55202895/article/details/126562933
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU