Pop Sequence
Given a stack which can keep
numbers at most. Push
numbers in the order of 1, 2, 3, ...,
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):
(the maximum capacity of the stack),
(the length of push sequence), and
(the number of pop sequences to be checked). Then
lines follow, each contains a pop sequence of
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.
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
My C program is following.
//Pop Sequence
#include <stdio.h>
#include <stdlib.h>
int possible(int* s, int M, int N);
int maxlessthantemp(int* t, int temp);
int main(void)
{
int M, N, K, i, j;
scanf("%d %d %d", &M, &N, &K);
int* p = (int*)malloc(K * sizeof(int));//To mark whether every one of these K sequences
is a possible pop sequence of the stack
int* seq = (int*)malloc(N * sizeof(int));//To point to a specific sequence
for (i = 0; i < K; ++i)
{
for (j = 0; j < N; ++j)
{
scanf("%d", &seq[j]);
}
if (possible(seq,M,N))
{
p[i] = 1;//
Possible pop sequence. Set
p[i] to 1
}
else
{
p[i] = 0;//
Impossible. Set
p[i] to 0
}
}
for (i = 0; i < K; ++i)
{
if (p[i])
{
printf("YES\n");//Possible to pop. Print "YES"
}
else
{
printf("NO\n");
//Impossible to pop. Print "YES"
}
}
return 0;
}
//To judge whether a specific sequence is possible to pop
int possible(int* s, int M, int N)
{
int i;
if (*s > M)
{
return 0;//The first element to pop is *s, which means elements 1~*s have been pushed. If the number of elements in the stack(*s) is more than M, this sequence doesn't match related
demand
}
int temp = *s;//To mark the popping element. *s is popped now.
int* t = (int*)malloc(N * sizeof(int));//
To mark which element(s) have/has been popped in this sequence
for (i = 0; i < N; ++i)
{
*(t + i) = i+1;//Not popped yet. Set it to related element value
}
*(t-1+*s) = 0;//*s is popped. Set *(t-1+*s) to 0
for (i = 1; i < N; ++i)
{
if (*(s + i) < maxlessthantemp(t,temp))
{
return 0;//Not proper popping element. Return 0
}
/* In the circumstances of *(s + i) > maxlessthantemp(t, temp) (Only pushing operation may cause overflow
ing, constant popping
can not),
the number of elements currently having been pushed subtracts the number of those elements who have been popped, gaining the number of elements currently in the stack.
If it's more than M, this sequence can not be popped rightly
*/
if (*(s + i) > maxlessthantemp(t, temp)&& *(s + i) - i > M)
{
return 0;
}
*(t - 1 + *(s + i)) = 0;//Having been popped. Set it to 0
temp = *(s + i);//
To mark the popping element
}
return 1;
}
/*Proper popping element should meet either one of following two conditions:
1.More than the biggist popped element.
2.
Less than the biggist popped element, but in this condition, it should be the maximum of those having not been popped.
So, proper popping element in the function possible(int* s, int N) should be more than temp, or
the maximum of those haven't been popped among
less-than-temp elements
.
I defined a function maxlessthantemp(int* t, int temp) to mark
the maximum of those haven't been popped among
less-than-temp elements
, so proper popping element should be at least this value.
*/
int maxlessthantemp(int* t, int temp)
{
int i;
//Through the following loops, element i(also *(t+i-1))becomes
the maximum of those haven't been popped among
less-than-temp elements
. Of course, the circumstance i equals 0 should be explained additionally.
for (i=temp-1;i>0&&*(t+i-1)==0;--i);
return i;//When i equals 0, set the value to 0
}