#include<stdio.h>
#include<stdlib.h>
#define MAX 1000
typedef struct node
{
int data[MAX];
int top;
}stack;
stack *createstack()
{
stack *s;
s=(stack *)malloc(sizeof(stack));
s->top=-1;
return s;
}
void push(stack *s,int item)
{
if(s->top==MAX-1)
{
printf("堆满\n");
return;
}
else
s->data[++s->top]=item;
}
void pop(stack *s)
{
if(s->top==-1)
{
printf("堆空\n");
return;
}
else
s->top--;
}
int check(int a[],int N,int M)
{
stack *s;
int i=0,num=0;//i为计数不大于N,num为要处理的数字
s=createstack();
push(s,0);//初始栈不为空,top=0,存入0
while(i!=N)
{
if(s->top>M)//栈满
return 0;
else
{
if(s->data[s->top]<a[i])
push(s,++num);
else if(s->data[s->top]==a[i])
{
pop(s);
i++;
}
else
return 0;
}
/*if(s->data[s->top]<a[i]&&s->top<M)
push(s,++num);
else if(s->data[s->top]==a[i])
{
pop(s);
i++;
}
else
return 0;*/
}
return 1;
}
int main()
{
int **a,K,N,M,i,k;
scanf("%d %d %d",&M,&N,&K);
a=(int **)malloc(K*sizeof(int *));
for(i=0;i<K;i++)
a[i]=(int *)malloc(N*sizeof(int));
for(k=0;k<K;k++)
{
for(i=0;i<N;i++)
scanf("%d",&a[k][i]);
}
for(k=0;k<K;k++)
{
if(check(a[k],N,M))
printf("YES\n");
else
printf("NO\n");
}
free(a);
return 0;
}
02-线性结构3 Pop Sequence
最新推荐文章于 2024-09-07 12:57:10 发布
