7-3 Pop Sequence (25分)
参考了博客 https://blog.csdn.net/whzyb1991/article/details/46663867
但是我看不懂(╥╯^╰╥)
题目链接 :https://pintia.cn/problem-sets/16/problems/665
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
作者: 陈越
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int n,m,k,i;
cin>>m>>n>>k;
while(k--) {
stack<int>s;
vector<int>v;
for (i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x);
}
s.push(0);//垫底的 这是看了另外一篇博客这样写的
//这样就不用判断栈是不是空了 感觉挺好的
// 一开始先放一个没什么用处的数据
int idx = 0, fl = 0;
int num = 1;
for (idx = 0; idx < n; idx++) {
//vector 的下标
// 从左往右找我们要的数字 比如说 5 6 4 3 7 2 1 第一个要找的就是 5
while (s.top() != v[idx]) {
//如果栈顶不是我们要找的数字
//就一直往栈里面放数字 题目要求是1 2 3 4 5 这样子放到
// 就用num一直加一就行了
s.push(num++);
if (s.size() > m+1) {
// 如果放进去这个数字之后,容量超出了 这是不行的
// 因为一开始放了个没用的数字进去 我们假定容量应该比题目给的 多一
// 也就是 m+1
fl = 1;
break;
}
}
if (fl)break; // 前面发现他是超出容量的 直接退出了
s.pop();// 找到了就把他弹出来
}
if (fl)printf("NO\n");
else printf("YES\n");
}
}
```cpp