02-线性结构4 Pop Sequence (25分)

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>
#define maxsize 1000
typedef struct SNode *Stack;
struct SNode
{
	int *data;
	int top;
	int MaxSize;
};

这是我们要实现的函数:

void push(Stack s,int N);//入栈
int pop(Stack s);//出栈
int compare(int *oringal,int *This,int M,int N);//比较
int Isempty(Stack s);/判断栈是否为空
int Isfull(Stack s);//判断栈是否为满
Stack createS(int MaxSize);//创造一个容量为MaxSize的栈

我们按照顺序来一 一实现,栈是按照先进后出的原则存入元素和输出元素.栈是有一个数组,有着最大容量,还有一个top来标记当前的元素个数.
我们先看入栈的函数:

void push(Stack s,int N)
{
	if(Isfull(s))printf("null");//如果为空,则直接	
	else if(s->top<s->MaxSize)//小于数组的最大容量
	{
		s->data[++(s->top)]=N;//top指向下一个位置并存入元素
	}
}

再看一下关于出栈的函数:
出栈要求栈里必须要有元素

int pop(Stack s)
{
	if(Isempty(s))return 0;//为空直接返回
	else return(s->data[s->top--]);//top往下移一位
}
int Isempty(Stack s)/判断栈是否为空
{
	return (s->top==-1);//刚开始top赋值为-1
}
int Isfull(Stack s)//判断栈是够为满
{
	return (s->top==s->MaxSize-1);//因为是数组所以要减一.
}
Stack createS(int MaxSize)//建栈,传入一个最大容量的值
{
	Stack s;
	s=(Stack)malloc(sizeof(struct SNode));
	s->data=(int *)malloc(MaxSize*sizeof(int));//动态创造数组的大小
	s->top=-1;
	s->MaxSize=MaxSize;
	return s;
}

本题目的主函数为:

int main()
{
	int M,N,K;
	Stack s;
	int i;
	int a[maxsize],b[maxsize];
	scanf("%d %d %d",&M,&N,&K);
	for(i=0;i<N;i++)
		a[i]=i+1;//原始的数组
		while(K--){
			for(i=0;i<N;i++)
			{
				scanf("%d",&b[i]);//当前数组赋值
			}
			if(compare(a,b,M,N))//原始数组和当前数组进行比较
				printf("YES\n");
			else
				printf("NO\n");
		}
	return 0;

}

比较函数

int compare(int *oringal,int *This,int M,int N)//判断是否能按照我们输入的进行输出元素
{
	Stack s;
	int num;
	int i=0,j=0;
	s=createS(M);//创造一个栈
	while(j<N)
	{
		if(i>=N)i=N-1;
		if(This[j]>oringal[i])
		{
			if(Isfull(s))return 0;
			else push(s,oringal[i++]);
		}
		else if(This[j]==oringal[i])
		{
			if(Isfull(s))return 0;
			else {push(s,oringal[i++]);
			pop(s);
			j++;
			}
		}
		else
		{
			if(Isempty(s))return 0;
			else{num=pop(s);
			if(num==This[j]) j++;
			else return 0;
			}
		}
	}
	return 1;
}

文章的总代码:

#include<stdio.h>
#include<stdlib.h>
#define maxsize 1000
typedef struct SNode *Stack;
struct SNode
{
	int *data;
	int top;
	int MaxSize;
};
void push(Stack s,int N);
int pop(Stack s);
int compare(int *oringal,int *This,int M,int N);
int Isempty(Stack s);
int Isfull(Stack s);
Stack createS(int MaxSize);
int main()
{
	int M,N,K;
	Stack s;
	int i;
	int a[maxsize],b[maxsize];
	scanf("%d %d %d",&M,&N,&K);
	for(i=0;i<N;i++)
		a[i]=i+1;
		while(K--){
			for(i=0;i<N;i++)
			{
				scanf("%d",&b[i]);
			}
			if(compare(a,b,M,N))
				printf("YES\n");
			else
				printf("NO\n");
		}
	return 0;

}
int Isfull(Stack s)
{
	return (s->top==s->MaxSize-1);
}
int Isempty(Stack s)
{
	return (s->top==-1);
}
Stack createS(int MaxSize)
{
	Stack s;
	s=(Stack)malloc(sizeof(struct SNode));
	s->data=(int *)malloc(MaxSize*sizeof(int));
	s->top=-1;
	s->MaxSize=MaxSize;
	return s;
}
int compare(int *oringal,int *This,int M,int N)
{
	Stack s;
	int num;
	int i=0,j=0;
	s=createS(M);
	while(j<N)
	{
		if(i>=N)i=N-1;
		if(This[j]>oringal[i])
		{
			if(Isfull(s))return 0;
			else push(s,oringal[i++]);
		}
		else if(This[j]==oringal[i])
		{
			if(Isfull(s))return 0;
			else {push(s,oringal[i++]);
			pop(s);
			j++;
			}
		}
		else
		{
			if(Isempty(s))return 0;
			else{num=pop(s);
			if(num==This[j]) j++;
			else return 0;
			}
		}
	}
	return 1;
}
int pop(Stack s)
{
	if(Isempty(s))return 0;
	else return(s->data[s->top--]);
}
void push(Stack s,int N)
{
	if(Isfull(s))printf("null");
	else if(s->top<s->MaxSize)
	{
		s->data[++(s->top)]=N;
	}
}
原创文章 7 获赞 2 访问量 328

猜你喜欢

转载自blog.csdn.net/shenbossed/article/details/106185779