数据结构习题Pop Sequence的理解----小白笔记^_^
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
题目大意为给定一个入栈序列1,2,...,N,限定栈的大小为M,要求判断输出序列的合法性。编程小白的我开始想的是找出出栈序列有哪些特征,然后根据这些出栈序列的特征判断一个栈的合法性。
可是弄了半天,写出的程序又复杂又没有什么条理,也没用到栈结构,而且还通不过。。。明明想一想就能判别的东西,用程序写出来总不尽人意。 无奈上网搜了一下大神们的博客,顿
觉豁然开朗。这道题考察的其实就是堆栈的基本概念和操作。比如说 5 6 4 3 7 2 1这个序列,首先我们自然由第一个出栈元素为5,推测出5出栈前栈里面应该为5 4 3 2 1(5为栈顶元素)。然
后5出栈,这时根据第二个元素为6知道,栈应该是继续讲6入栈的,此时栈顶元素等于6,将6出栈。依次类推,只要比较栈顶元素和将要出栈的元素的大小,就可以知道该进栈还是出栈。当
然,若是出现栈顶元素大于将要出栈的元素,或者虽然栈顶元素小于将要出栈的栈顶元素但此时栈已满都说明这个将要出栈的元素是不可能出现的。因此可以判断该出栈序列不合法。以上就是
根据出栈序列,推测出栈和入栈操作的过程,关键是比较栈顶元素与“将要出栈元素”的大小。根据大神们的博客的思路再结合自己的理解自己独立地(就是没有边看边写^_^)写出了如下的代码。
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 typedef struct Node { //c语言栈基本结构的构造 5 int *Data; 6 int Top; 7 int MaxSize; 8 }*Stack; 9 10 int M, N, K; //全局变量,放在栈结构后,方便子函数使用 11 12 Stack CreateStack(int MaxSize) { 13 Stack S = (Stack)malloc(sizeof(struct Node)); 14 S->Data = (int *)malloc(MaxSize * sizeof(int)); 15 S->Top = -1; 16 S->MaxSize = MaxSize; 17 return S; 18 } 19 20 void Push(Stack S, int ele) { 21 if (S->Top == S->MaxSize) { //短路写法,遇错就返回,提高程序可读性 22 printf("FULL\n"); 23 return; 24 } 25 S->Data[++(S->Top)] = ele; 26 } 27 28 void Pop(Stack S) { 29 if (S->Top == -1) { 30 printf("Empty\n"); 31 return; 32 } 33 S->Top--; 34 } 35 36 int top(Stack S) { //返回栈顶元素 37 return S->Data[S->Top]; 38 } 39 40 int StackCheck(int *v,Stack S) { //检查输出序列是否为容量为M的栈输出序列 41 int idx = 0; //数组v的指标,指向下一个将要出栈的元素 42 int num = 1; //按顺序进栈的元素,进栈自动加1 43 S->Top = -1; 44 while (idx != N) { 45 while (S->Top != -1 && top(S)<v[idx]&&S->Top+1<M||S->Top==-1&&idx!=N) { 46 Push(S, num++); //若栈非空且栈顶元素小于将要比较的出栈元素或者栈为空且idx未滑至N(即出栈序列未比较完成) 47 } 48 if (S->Top!=-1&&top(S) == v[idx]) { //栈非空且栈顶元素值等于将要比较的出栈元素 49 Pop(S); 50 idx++; 51 } 52 if (S->Top != -1&&top(S)>v[idx]|| S->Top + 1 == M&&top(S) != v[idx]) { //栈非空且栈顶元素大于将要比较的出栈元素或栈满但栈顶元素仍小于将要比较的出栈元素 53 return 0; //直接返回,使程序更易理解 54 } 55 } 56 57 return 1; 58 } 59 60 int main() { 61 int i, j; 62 Stack S; 63 64 scanf("%d %d %d", &M, &N, &K); 65 int *v = (int *)malloc(N * sizeof(int)); 66 S = CreateStack(M); 67 for (i = 0;i < K;i++) { 68 for (j = 0;j < N;j++) { 69 scanf("%d", v + j); 70 } 71 if (StackCheck(v, S)) 72 printf("YES\n"); 73 else 74 printf("NO\n"); 75 } 76 77 }