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");
}
}