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
代码(链表实现):
#include <stdio.h>
#include <stdlib.h>
/*
思路:
要实现的是给定入栈顺序的堆栈的随机输入和输出
do{
if(top=='s')//s为此时答案中应输出的东西
{
POP;
flag=0;
}
else
{
PUSH;
flag=1;
}
}while(num<=N && lenth<=M);
if(flag==0){printf("YES");}
else{printf("NO");}
*/
#define bool int
#define true 1
#define false 0
typedef struct Snode *Stn;
struct Snode{
int lenth;
int TOP;
Stn next;
};
typedef struct Snode *Stack;
Stack CreatStack(void){/*建立空的头结点,返回该头结点*/
Stack S;
S = (Stack)malloc(sizeof(struct Snode));
S->next = NULL;
S->lenth = 0;
return S;
}
bool IsEmpty(Stack S)
{/*空栈则返回true,非空返回false*/
return (S->next == NULL);
}
bool PUSH(Stack S,int i)
{
Stn node;
node = (Stn)malloc(sizeof(struct Snode));
if(IsEmpty(S))
{
node->lenth = 1;
}
else
{
node->lenth = S->next->lenth+1;///不知道噶不该有这个括号(可以不加)
// printf("S->next->lenth = %d ",S->next->lenth);
}
node->TOP = i;
node->next = S->next;
S->next = node;
return true;
}
bool POP(Stack S)
{
Stn node;
node = (Stn)malloc(sizeof(struct Snode));
if(IsEmpty(S))
{
printf("栈空");
return false;
}
else
{
node = S->next;
S->next = node->next;
}
return true;
}
int main(int argc,char argv[])
{
int M,N,K,numflag = 0;
scanf("%d %d %d",&M,&N,&K);
/*M:the maximum capacity of the stack, N:the length of push sequence, K:the number of pop sequences to be checked*/
while(K--)
{
int i,flag=-1,j;
int *a;
a = (int*)malloc(N*sizeof(int));
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
// printf("t ");
}
Stack S=CreatStack();
j=0;
for(i=1;i<=N+1;i++)
{
if(S->next && (S->next->lenth)>M)
{
break;
}
while(S->next && S->next->TOP == a[j])
{
// printf("POP ");
// printf("S->TOP= %d a[%d]=%d\n",S->next->TOP,j,a[j]);
POP(S);
j++;
flag=0;
}
// printf("S->next->lenth = %d S->TOP= %d ",S->next->lenth,S->next->TOP);
if(IsEmpty(S) && i>N)
{
break;
}
// printf("PUSH ");
PUSH(S,i);
// printf("S->next->lenth = %d S->TOP= %d a[%d]=%d\n",S->next->lenth,S->next->TOP,j,a[j]);
flag=1;
}
if(numflag)
{
printf("\n");
}
else
{
numflag=1;
// printf("numflag = %d ",numflag);
}
if(flag==0) //最后一步是POP
{
printf("YES");
}
else if(flag==1) //最后一步是PUSH
{
printf("NO");
}
}
return 0;
}