全部每周作业和视频思考题答案和解析 见 浙江大学 数据结构 思考题+每周练习答案汇总
题目: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.
翻译:给定一个最多能保存M个数的堆栈。按1,2,3,…,N的顺序推N个数字,然后随机弹出。您应该知道给定的数字序列是否可能是堆栈的弹出序列。例如,如果M是5,N是7,我们可以从堆栈中获得1,2,3,4,5,6,7,但不能从3,2,1,7,5,6,4。
输入规格:
每个输入文件包含一个测试用例。对于每种情况,第一行包含3个数字(都不超过1000):M(堆栈的最大容量)、N(推送序列的长度)和K(要检查的pop序列的数量)。接下来是K行,每行包含N个数字的pop序列。一行中的所有数字都用空格隔开。
输出规格:
对于每个弹出序列,如果确实是堆栈的可能弹出序列,则在一行中打印“是”,如果不是,则打印“否”。
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
解题思路:
这个题考察的是栈的基本结构,在这里我进行一下剖析。
我们以输入数据的第二行为例,第一个打印出来3,所以我们应该把1和2push进栈,然后push并pop 3这个数字,第二个数字是2, 2的特点是小于3,所以不可能再push后面的数,所以得pop 2,所以如果输出序列是 3 1 2 x x x x 我们就知道这是不可能的,因为3后面若想输出1,还隔着一个 2 。然后是7,注意7是可能的,因为这个时候栈是空的,所以可以把4 5 6 7 依次push进去,并输出7。但是想输出5,必须先输出6,所以错误。
分析完以后,我们开始构建程序:
#include<iostream>
using namespace std;
int main() {
int K;//K组序列
int N;//每个序列的数据数
int M;//栈的大小
cin >> K >> N >> M;
stack<int> mystack;
for (int i = 0;i < K;i++) {
//依次处理每行的数据
}
system("pause");//暂停查看结果(Visual Studio上使用的)
return 0;
}
程序框架大致如此。在一开始的时候定义一个栈,现在开始构建解析输入数据每一行的程序。
int current;
int sum = 0;//用来记录栈内的元素数
int pushRe = 1;//用来记录将要push到过栈里面的数据
int inputRe = 0;//记录输入进去的数据数量
current用来每次读取一个数,然后sum的作用是记录栈内的元素一共有多少个。pushRe是记录下一个将push进栈里的数据是什么,因为数据是从1开始不断push进栈的,所以每次push以后pushRe加1就行了。
然后开始一个一个读入处理:
for (int j = 0;j < N;j++) {
cin >> current;
inputRe++;
if (mystack.empty()) {
mystack.push(pushRe);
sum++;
pushRe++;
}
}
每次读入一个数以后,记录读入个数的inputRe都加一。之所以要记录读入个数是因为我们比如把每行的数据都读完。
然后再这个for循环后面进行判断:
while (inputRe < N) {
cin >> current;
inputRe++;
}
如果没有读完,就继续读。(因为可能还没读完就判断出这是错误的情况了,然后跳出了大循环)
之后在这个for循环里,要判断:
if (mystack.empty()) {
mystack.push(pushRe);
sum++;
pushRe++;
}
if (current > mystack.top()) {
for (int k = pushRe;k <= current;k++) {
mystack.push(k);
sum++;
pushRe++;
}
}
如果栈是空的,废话少说先push进去一个当前应该push的数据进去,不然怎么指望pop出current同值数据呢?
如果读入的数据值大于mystack.top(),说明还得继续push,push到等于current才能pop出current同值数据。
如果读入数据小于mystack.top(),那这种情况一定是错误的。
之后判断,其实判断很简单:
if (sum > M) { //打印一次就够了
cout << "NO" << endl;
break;
}
if (mystack.top() != current) {
cout << "NO" << endl;
break;
}else {
sum--;
mystack.pop();
}
if (pushRe == N + 1 && sum == 0) {
cout << "YES" << endl;
}
栈数据大于M,则错误。如果顶部数据不是current,那也是错的。否则就弹出去,同时栈元素数减一。
如果当最后我们已经push了N个数了(pushRe记录的是将要去pushRe的数,其值减1位push的数的个数),并且栈内也是空的,证明该顺序是可行的。
在循环最后还得清空栈:
while (!mystack.empty()) {
mystack.pop();
}
出现过的bug:
一开始没有把每行数据都读完就break,然后处理下一次循环,导致读入混乱。一定要注意每个循环中把每行都用cin读完。
一开始以为第一行读入的三个数据顺序是K N M,结果调试了好久才发现错误。
现在给出最终代码:
#include<iostream>
#include<stack>
using namespace std;
int main() {
int K;//K组序列
int N;//每个序列的数据数
int M;//栈的大小
cin >> M >> N >> K;
stack<int> mystack;
for (int i = 0;i < K;i++) {
//依次处理每行的数据
int current;
int sum = 0;//用来记录栈内的元素数
int pushRe = 1;//用来记录将要push到过栈里面的数据
int inputRe = 0;//记录输入进去的数据数量
for (int j = 0;j < N;j++) {
cin >> current;
inputRe++;
if (mystack.empty()) {
mystack.push(pushRe);
sum++;
pushRe++;
}
if (current > mystack.top()) {
for (int k = pushRe;k <= current;k++) {
mystack.push(k);
sum++;
pushRe++;
}
}
if (sum > M) { //打印一次就够了
cout << "NO" << endl;
break;
}
if (mystack.top() != current) {
cout << "NO" << endl;
break;
}else {
sum--;
mystack.pop();
}
if (pushRe == N + 1 && sum == 0) {
cout << "YES" << endl;
}
}
while (inputRe < N) {
cin >> current;
inputRe++;
}
while (!mystack.empty()) {
mystack.pop();
}
}
system("pause");
return 0;
}
测试全部通过。