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
题意理解
给出一个容量为M的堆栈。以1,2,3…N的顺序入栈N个数字并随机出栈。你需要判断出给定的数字序列是否是可能的出栈序列。例如,如果M=5,N=7,则我们可以从得出1,2,3,4,5,6,7的出栈数字序列,但无法得到3,2,1,7,5,6,4的序列。
输入格式:
第一行:M - 堆栈最大容量,N - 序列中元素的个数,K - 数字序列的个数;
余下:数字序列;
输出格式:可能就输出“YES”,不可能则输出“NO”;
代码如下:(Java)
ps:第一次写用的投机取巧的方式,比较大小写的,结果 被卡特殊算法卡住了;后来改用的模拟堆栈push和pop的方式做的;
import java.io.BufferedReader;
import java.io.FileDescriptor;
import java.io.FileReader;
import java.io.IOException;
/**
* 思路:对堆栈的理解
* 模拟堆栈,按照给定的顺序pop,若违反先进后出原则,则错误
*/
public class Main {
private int capacity ;
private int len;
private int groupNum;
public static void main(String[] args){
Main main2 = new Main();
String[] inputs = main2.readInput();
for (String str : inputs) {
System.out.println(main2.possibility(str));
}
}
//读取控制台
public String[] readInput() {
BufferedReader br = new BufferedReader(new FileReader(FileDescriptor.in));
String[] results = null;
try {
String info = br.readLine();
String[] infos = info.split(" ");
capacity = Integer.parseInt(infos[0]);
len = Integer.parseInt(infos[1]);
groupNum = Integer.parseInt(infos[2]);
results = new String[groupNum];
for (int i = 0; i < groupNum; i++) {
String s = br.readLine();
results[i] = s;
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return results;
}
//判断组合存在的可能性
public String possibility(String str){
//模拟堆栈头指针
MyStack myStack= new MyStack(capacity);
String[] numStr = str.split(" ");
//输出组合的下标
int flag = 0;
//要push进数组的值
int i = 1;
//转换成int数组
while (flag < len){
//获得栈顶元素的值
Integer temp = myStack.getTopNum();
//情况一:栈不空且栈顶元素=给出的已经pop出的元素
if (temp != null && temp == Integer.parseInt(numStr[flag].trim())){
//正常pop出,若pop时有问题,则返回NO
if(!myStack.pop(temp)){
return "NO";
}
flag++;
//push进数组的元素已经超过给定长度,还无法pop出组合中按顺序应pop出的值,返回NO
}else if(i == len+1){
return "NO";
}else{
//不能pop且栈未满,则入栈
if(!myStack.push(i)){
return "NO";
}
i++;
}
}
return "YES";
}
}
//堆栈类
class MyStack{
//堆栈容量
private int cap;
//用数组模拟栈内空间
private int[] data;
//堆栈头指针
private int top = -1;
//构造方法
public MyStack(int cap) {
this.cap = cap;
data = new int[cap];
}
//获得堆栈中元素个数
public int getLen(){
return this.top+1;
}
//入栈操作
public boolean push(int num){
if (this.top+1>=this.cap){
return false;
}
data[this.top+1] = num;
this.top++;
return true;
}
//出栈操作
public boolean pop(int num){
if(num != data[this.top]){
return false;
}
data[this.top] = 0;
this.top--;
return true;
}
//获得栈顶元素值
public Integer getTopNum(){
if (this.top == -1){
return null;
}
return data[this.top];
}
}