1051 Pop Sequence 25
题目描述
Given a stack which can keep M M M numbers at most. Push N N N numbers in the order of 1, 2, 3, …, N N 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 M M is 5 and N N 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 M M (the maximum capacity of the stack), N N N (the length of push sequence), and K K K (the number of pop sequences to be checked). Then K K K lines follow, each contains a pop sequence of N N 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
问题思路
本题大意:给定一个容量为
M
M
M的栈和
K
K
K个由1, 2, 3, ... N
随机组成的序列,判断其是否可能是由1, 2, 3, ... N
的顺序入栈输出的序列。
思路一:(逆向还原) 戳我看源码
这个思路是我最开始看完题目想到的,相比于第二个思路复杂一点。
题目要求我们判断一个序列是否是经堆栈处理的序列,而原序列是已知的:1, 2, 3, ... N
。所以我们可以从这里出发:能不能将序列进行堆栈逆向操作,还原出这个序列的原序列,将其与1, 2, 3, ... N
进行比较?
答案是可以的,因为序列本身就包含了堆栈操作的信息,对于这个问题:
- 如果一个子序列递减,那这个子序列一定是连续的出栈操作构成的
- 如果相邻元素递增,那这两个元素出栈操作之间一定是连续的入栈操作
基于以上思考,我们可以这么做:
- 从序列末尾开始扫描,如果元素向前递增,则入栈(对应正向的出栈操作);若遇到递减的元素,比较这个元素和栈顶元素,如果这个元素小于栈顶元素,则出栈(对应正向的入栈操作),重复操作直到这个元素大于栈顶元素,入栈,继续向前扫描。
- 每次入栈操作,检查是否栈满,栈满则出栈一次。
- 最后检查逆向出的原序列与
1, 2, 3, ..., N
是否相等
思路二:(正向模拟) 戳我看源码
这个思路简单直接,直接将1, 2, 3, 4, 5, 6, 7
依次入栈进行模拟即可。惭愧的是,我并没有想到这个方法。
- 用一个变量记录输出序列的当前待出栈元素,初始时为第一个元素。
1, 2, 3, ..., N
依次入栈,检查栈顶元素是否等于待出栈元素,若相等,出栈并让待出栈元素后移一个,循环这一操作直到栈顶元素不等于待出栈元素或者栈空;若不相等,继续入栈。- 每次入栈,检查是否栈溢出,若溢出说明序列不符合条件。
- 最后检查栈是否为空,若不为空,说明序列不符合条件。
注:对于每个序列进行操作之前需要清空堆栈,以免影响操作。
源码
// 思路一
#include <cstdio>
#include <stack>
using namespace std;
inline bool isFull(stack<int> &s, int capacity)
{
return s.size() == capacity;
}
int main()
{
int M, N, K;
stack<int> s1;
stack<int> s2;
scanf("%d %d %d", &M, &N, &K);
int res[N], reverse[N], tmp;
for (int i = 0; i < K; i++)
{
for (int j = 0; j < N; j++)
{
scanf("%d", &res[j]);
}
for (int j = N - 1; j >= 0; j--)
{
tmp = res[j];
while (!s1.empty() && (tmp < s1.top() || isFull(s1, M)))
{
s2.push(s1.top());
s1.pop();
}
s1.push(tmp);
}
while (!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
bool flag = true;
for (int j = 1; j <= N; j++)
{
if (j != s2.top())
{
flag = false;
break;
}
s2.pop();
}
printf(flag ? "YES\n" : "NO\n");
}
return 0;
}
// 思路二
#include <cstdio>
#include <stack>
using namespace std;
int main()
{
int M, N, K;
stack<int> s;
scanf("%d %d %d", &M, &N, &K);
int res[N], sim_res[N], next, sim_next;
for (int i = 0; i < K; i++)
{
for (int i = 0; i < N; i++)
{
scanf("%d", &res[i]);
}
bool flag = true;
next = 0, sim_next = 0;
for (int i = 1; i <= N; i++)
{
s.push(i);
if (s.size() > M)
{
flag = false;
break;
}
while (!s.empty() && s.top() == res[next])
{
sim_res[sim_next++] = s.top();
s.pop();
next++;
}
}
if (!s.empty())
flag = false;
printf(flag ? "YES\n" : "NO\n");
while (!s.empty())
{
s.pop();
}
}
return 0;
}