数据结构习题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出栈。依次类推,只要比较栈顶元素和将要出栈的元素的大小,就可以知道该进栈还是出栈。当
然,若是出现栈顶元素大于将要出栈的元素,或者虽然栈顶元素小于将要出栈的栈顶元素但此时栈已满都说明这个将要出栈的元素是不可能出现的。因此可以判断该出栈序列不合法。以上就是
根据出栈序列,推测出栈和入栈操作的过程,关键是比较栈顶元素与“将要出栈元素”的大小。根据大神们的博客的思路再结合自己的理解自己独立地(就是没有边看边写^_^)写出了如下的代码。
#include<stdio.h>
#include<stdlib.h> typedef struct Node { //c语言栈基本结构的构造
int *Data;
int Top;
int MaxSize;
}*Stack; int M, N, K; //全局变量,放在栈结构后,方便子函数使用 Stack CreateStack(int MaxSize) {
Stack S = (Stack)malloc(sizeof(struct Node));
S->Data = (int *)malloc(MaxSize * sizeof(int));
S->Top = -;
S->MaxSize = MaxSize;
return S;
} void Push(Stack S, int ele) {
if (S->Top == S->MaxSize) { //短路写法,遇错就返回,提高程序可读性
printf("FULL\n");
return;
}
S->Data[++(S->Top)] = ele;
} void Pop(Stack S) {
if (S->Top == -) {
printf("Empty\n");
return;
}
S->Top--;
} int top(Stack S) { //返回栈顶元素
return S->Data[S->Top];
} int StackCheck(int *v,Stack S) { //检查输出序列是否为容量为M的栈输出序列
int idx = ; //数组v的指标,指向下一个将要出栈的元素
int num = ; //按顺序进栈的元素,进栈自动加1
S->Top = -;
while (idx != N) {
while (S->Top != - && top(S)<v[idx]&&S->Top+<M||S->Top==-&&idx!=N) {
Push(S, num++); //若栈非空且栈顶元素小于将要比较的出栈元素或者栈为空且idx未滑至N(即出栈序列未比较完成)
}
if (S->Top!=-&&top(S) == v[idx]) { //栈非空且栈顶元素值等于将要比较的出栈元素
Pop(S);
idx++;
}
if (S->Top != -&&top(S)>v[idx]|| S->Top + == M&&top(S) != v[idx]) { //栈非空且栈顶元素大于将要比较的出栈元素或栈满但栈顶元素仍小于将要比较的出栈元素
return ; //直接返回,使程序更易理解
}
} return ;
} int main() {
int i, j;
Stack S; scanf("%d %d %d", &M, &N, &K);
int *v = (int *)malloc(N * sizeof(int));
S = CreateStack(M);
for (i = ;i < K;i++) {
for (j = ;j < N;j++) {
scanf("%d", v + j);
}
if (StackCheck(v, S))
printf("YES\n");
else
printf("NO\n");
} }
随机推荐
- [AHOI 2013]差异
Description 题库链接 给定一个长度为 \(n\) 的字符串 \(S\) ,令 \(T_i\) 表示它从第 \(i\) 个字符开始的后缀.求 \[\sum_{1\leqslant i< ...
- java通过url在线预览Word、excel、ppt、pdf、txt文档
java通过url在线预览Word.excel.ppt.pdf.txt文档中的内容[只获得其中的文字] 在页面上显示各种文档中的内容.在servlet中的逻辑 word: BufferedInputS ...
- find the most comfortable road(hdu1598)不错的并查集
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- N皇后问题hdu2553(dfs)
N皇后问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- Android - AMS源码分析
Android核心分析之AMS App和AMS(SystemServer进程)还有zygote进程分属于三个独立的进程 App与AMS通过Binder进行IPC通信,AMS(SystemServer进 ...
- 4.Factory Pattern(工厂模式)
工厂模式(Factory Pattern)定义: 定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个.工厂方法让类把实例化推迟到子类. 针对实现编程,但是当我们每次使用new时候,不正是在针对 ...
- bzoj1758Wc10重建计划——solution
1758: [Wc2010]重建计划 Time Limit: 40 Sec Memory Limit: 162 MBSubmit: 4707 Solved: 1200[Submit][Status ...
- 1.String、StringBuffer与StringBuilder之间区别
1.三者在执行速度方面的比较:StringBuilder > StringBuffer > String 2.String <(StringBuffer,StringBuild ...
- 正则匹配身份证有bug你知道么?
在开发中,我们需要验证用户的输入信息,多半采用正则验证,下面就是身份证证号的几种常用的正则表达式: var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x) ...
- js的style.display小问题
在元素添加class样式隐藏display:none; 使用console.log(xx.style.display);//空值