浙大数据结构第二周之02-线性结构4 Pop Sequence

题目详情:

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~n的数字,再给n个测试顺序,判断每个顺序能否实现

主要思路:

建立一个辅助栈,想这个辅助栈里压入1~n

给出的顺序无法实现有两种情况,一种是栈的空间不够大,比如栈的空间是5,给7 6 5 4 3 2 1就不能;一种是顺序问题

外层循环遍历从1~n,如果栈没有满,就压入栈

内层循环判断栈顶元素和cur指针当前所指元素是否相同,如果相同,就弹出栈顶元素,并且cur指针指向下一位

最后如果反应循环过程中栈没满的flag为true和辅助栈最后为空,说明该序列合法

原理……还没想好

第一次写错误:

判断栈有没有满不能用MAX_SIZE,而要定义一个全局变量读入题干所给的M

代码实现:

#include 
#include 
#define MAX_SIZE 1005
#define EMPTY -1
/*实现栈的数据结构*/
typedef struct StackNode Stack;
struct StackNode {
    int Data[MAX_SIZE];
    int Top;
};

/*栈的初始化*/
void InitStack(Stack* stackName) {
    (*stackName).Top = -1;
    return;
}
/*检测栈有没有满,返回false是没满,返回true是满了*/
int CAPACITY;
bool IsFull(Stack* stackName) {
    bool ret = false;
    if((*stackName).Top == CAPACITY - 1) ret = true;
    return ret;
}

/*检测栈是不是为空,返回true就是空,返回false就是不空*/
bool IsEmpyt(Stack* stackName) {
    bool ret = false;
    if((*stackName).Top == EMPTY) ret = true;
    return ret;
}

/*向栈里加入元素*/
void Push(Stack* stackName, int data) {
    if(IsFull(stackName)) {
        return;
    }

    int position = (*stackName).Top;
    (*stackName).Data[++position] = data;
    (*stackName).Top = position;
    return;
}

/*从栈里弹出元素*/
void Pop(Stack* stackName) {
    if(IsEmpyt(stackName)) {
        return;
    }
    
    int topPosition = (*stackName).Top;
    int topData = (*stackName).Data[topPosition--];
    (*stackName).Top = topPosition;
}

/*访问栈顶元素*/
int Top(Stack* stackName) {
    if(IsEmpyt(stackName)) {
        return EMPTY;
    }

    return (*stackName).Data[(*stackName).Top];
}
/*读入待判断数据,返回判断结果*/
bool Judge(const int* num, int dataLength) {
    Stack s;
    InitStack(&s);
    int cur = 0;
    bool flag = true;
    for(int i = 1; i <= dataLength; i++) {
        if(!IsFull(&s)) {
            Push(&s, i);
        }
        else {
            flag = false;
            break;
        }
        while(!IsEmpyt(&s) && num[cur] == Top(&s)) {
            Pop(&s);
            cur++;
        }
    }

    if(flag == true && IsEmpyt(&s)) {
        return true;
    }
    else return false;
}
int main() {
    int N, K;    //M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). 
    scanf("%d %d %d", &CAPACITY, &N, &K);
    int num[N];
    for(int i = 0; i < K; i++) {
        for(int j = 0; j < N; j++) {
            scanf("%d", &(num[j]));
        }

        if(Judge(num, N)) {
            printf("YES\n");
        }
        else {
            printf("NO\n"); 
        }
    }
    return 0;
}

你可能感兴趣的:(数据结构,数据结构)