Pop Sequence
题目
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 T 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
题目分析
题目大意是给定一个入栈序列1,2,3,…,N,限定栈的大小为***M***,要求判断序列是否有可能从该栈输出。
例如题目所给的样例:5 6 4 3 7 2 1:
- 我们根据第一个出栈元素为5可以判断在5出栈前,1 2 3 4都应已入栈,则下一个应入栈元素是6。
- 根据第二个元素为6可知,6出栈前栈里的元素为1 2 3 4 6;下一个进栈元素是7。
- 根据第三个元素是4,第四个元素是3可知,7进栈前栈里的元素是1 2 ;7出栈后2,1分别出栈。整个过程符合1,2,3,…,N的入栈顺序,故该序列合法。
因此,若出现【栈顶元素大于待出栈的元素】或者【栈顶元素小于待出栈的元素】但【栈已满】的情况,则说明这个待出栈的元素是不可能出现的,据此可以判断该出栈序列不合法。
代码实现
链表栈
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node
{
int *base;
int top;
int size;
}*stack;
int m, n, t;
stack CreateStack(int size)
{
stack s = (stack)malloc(sizeof(struct node));
s->base = (int*)malloc(size * sizeof(int));
if (!s->base)
{
cout << "ERROR" << endl;
return 0;
}
s->top = 0;
s->size = size;
return s;
}
void push(stack s,int e)
{
if (s->top == s->size)//栈已满
{
cout << "FULL" << endl;
return ;
}
s->base[++(s->top)] = e;
return ;
}
void pop(stack s)
{
if (s->top == 0)//栈为空
{
cout << "EMPTY" << endl;
return ;
}
s->top--;
}
int Check(int *array, stack s)
{
int now = 1;
int flag = 1;
s->top = 0;//注意这里要重新设置栈顶位置
for (int i = 0; i < n && flag; i++)
{
while ((s->base[s->top] != array[i] || s->top == 0)&&flag)//【栈为空】或【栈顶元素与待比较的出栈元素不等】
{
push(s, now);
if (s->top==s->size && s->base[s->top] != array[i]) //若【栈已满且栈顶元素仍然小于待比较的出栈元素】
{
flag = 0;
break;
}
now++;
}
if ( flag && s->base[s->top] == array[i] && s->top >= 1)//若【栈非空且栈顶元素等于待比较的出栈元素】
pop(s);
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 1;
}
int main()
{
cin >> m >> n >> t;
int *array = (int*)malloc(n * sizeof(int));
stack s = CreateStack(m);
while (t--)
{
for (int i = 0; i < n; i++)
cin >> array[i];
Check(array, s);
}
free(s->base);
free(array);
return 0;
}
数组栈
#include<iostream>
using namespace std;
int main()
{
int m, n, t;
cin >> m >> n >> t;
int stack[1005];//栈
while (t--)//遍历每一个序列
{
int top = 0;//栈顶元素
int num;//待比较的出栈元素
int flag = 1;//判断标志
int now = 1;
for (int i = 0; i < n; i++)//遍历每一个数
{
cin >> num;
while ((top == 0 || num != stack[top]) && flag)//栈为空或者待出栈元素与栈顶元素不相等
{
stack[++top] = now;
if (top > m)
{
flag = 0;
break;
}
now++;
}
if (num == stack[top] && flag && top >= 1)//栈不为空且......
{
top--;
}
}
if (flag)
printf("YES\n");
else
printf("NO\n");
}
}