1051. Pop Sequence (25)
来源:互联网 发布:漂亮的ava女演员 知乎 编辑:程序博客网 时间:2024/06/07 20:04
题目:
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.
Input Specification:
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.
Output Specification:
For each pop sequence, print in one line “YES” if it is indeed a possible pop sequence of the stack, or “NO” if not.
Sample Input:
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
Sample Output:
YES
NO
NO
YES
NO
解答:
//A1051 #include<cstdio>#include<stack>using namespace std;int main(){ int m,n,k; scanf("%d%d%d",&m,&n,&k); stack<int> st; int arr[1010]; while(k--){//k在后文不回再用到,可以用这种方式减少循环代码量 while(!st.empty())//每次输入前均清原有空栈 st.pop(); for(int i=1;i<=n;i++)//读入数据 scanf("%d",&arr[i]); int current=1; for(int i=1;i<=n;i++){ st.push(i); if(st.size()>m) break; //栈顶元素和当前arr要出列元素相同时不断弹出,current指针向后移 while(!st.empty()&&st.top()==arr[current]){ st.pop(); current++; } } if(current==n+1)printf("YES\n");//如果按顺序读完了所有的元素,说明没问题 else printf("NO\n"); } return 0;}
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- CPU状态信息us,sy,ni,id,wa,hi,si,st含义及分析
- ORA-12638: 身份证明检索失败
- 2017中国软件技术大会即将于12月在京召开!
- 设计模式之抽象工厂Abstract Factory
- 浅谈Django中的mptt
- 1051. Pop Sequence (25)
- LeetCode.102 Binary Tree Level Order Traversal
- Android源码基础解析之Activity销毁流程
- 记忆网络之End-To-End Memory Networks
- PHP中的 抽象类abstract和 接口类interface的区别
- 【Power Designer】反向工程生成类图
- 理解spark闭包
- leetcode--136--Longest Substring Without Repeating Characters
- ijkPlyer学习 ------ Demo运行