02-线性结构4 Pop Sequence

最近想着要把408看一下,然后陈越姥姥的数据结构这学期又开课了,然后就在拼题A上练习了一下,突然想起这个问题,你觉得你大学学得最好的科目是什么?回答数据结构是不是玩完了,哈哈(没有表情可以插入)。今天是一道关于堆栈的题,就是今天练习的,记得大二第一次接触数据结构的时候学堆栈,只有一个印象(先进后出),当时也没有进行什么练习,这道题不难,倒是很好的磋磨了一下。

切换频道啦

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,3…..N。下列出栈序列可能的输出YES,不可能的输出NO。当然啦,堆栈的大小是有限制的。
思路: 对每个序列单独处理,既然进栈序列是1-N,那就设置一个循环嘛(第一次循环,从1–N的顺序进栈嘛)。每次进栈一个数i,就将栈顶元素i和出栈序列进行比较,如果相同,就将栈顶元素i出栈,并就此刻的栈顶元素和出栈序列下一个数进行比较,如此循环直到遇到不相等或者堆栈中的元素已经为空的时候(第二层循环)。第二层循环出来之后,这个时候就需要将i+1入栈啦,当然,有一个前提条件,堆栈肯定不能满啦。然后再回到外层的循环,继续重复上面的步骤。
注意点:

  1. 堆栈满了,然而此刻栈顶元素和出栈序列还是不相等,直接返回false。
  2. 每次处理的序列,如果是自己定义的堆栈,用数组的话,记得在处理下一个序列的时候,一定要当前堆栈清空哈,不然会影响结果哟。用C++自带的堆栈类,我还不知道该怎么清空堆栈呢,所以我在第二个的时候,每处理一个序列,我都重新定义了堆栈。两个版本的代码我都会贴上,第二个版本是自己之前写的,堆栈是自己定义的,当时写代码的时候,没怎么注意那个模块化,看起来有点不舒服,(虽然现在也没好到哪里去。。。捂脸(/ω\)),大家理解理解,嘿嘿。
#include <iostream>
#include<stack>
using namespace std;
#define Max 1000
int Size,N,K;//堆栈的大小,数的个数,需要check的序列个数
int List[Max][Max];//存放输入的序列数
//输入函数,接收序列
void Input(){
    int i,j;
    for(i=1;i<=K;i++){
        for(j=1;j<=N;j++){
            cin>>List[i][j];
        }
    }
}
//对每个序列进行处理
bool Process(int List[])
{
    //定义堆栈
    stack<int> Stack;
    int i,j=1;
    //从1到N依次进栈
    for(i=1; i<=N; i++)
    {
        Stack.push(i);
        //如果栈不为空,就将栈顶元素和出栈序列比较
        while(!Stack.empty())
        {
            if(Stack.top()==List[j])
            {
                j++;
                Stack.pop();
                continue;
            }
            break;
        }
        //注意堆栈的大小是有限制的
        if(Stack.size()<Size)
        {
            continue;
        }
        else
        {
            return false;
        }
    }
    if((j-1)==N)
    {
        return true;
    }
    else{
        return false;
    }

}
int main()
{
    cin>>Size>>N>>K;
    Input();
    for(int i=1;i<=K;i++){
        if(Process(List[i])){
            cout<<"YES"<<endl;
        }
        else{
            cout<<"NO"<<endl;
        }
    }
    return 0;
}
#include <iostream>
#include<malloc.h>
using namespace std;
#define MaxSize 1000
typedef struct
{
    int data[MaxSize];
    int top;
} SqStack;
void InitStack(SqStack *&s)
{
    s=(SqStack *)malloc(sizeof(SqStack));
    s->top=0;
    s->data[s->top]=0;
}
//定义进栈函数
bool Push(SqStack *&s,int e,int M)
{
    if(s->top==M)
    {
        return false;
    }
    s->top++;
    s->data[s->top]=e;
    return true;
}
//定义出栈函数
bool Pop(SqStack *&s,int &e)
{
    if(s->top==0)
    {
        return false;
    }
    e=s->data[s->top];
    s->top--;
    return false;
}
int main()
{
    /*堆栈的长度*/
    int M;
    /*要测试的个数*/
    int N;
    //定义需要比较的数组个数
    int K;
    //定义计数器
    int i,j,b;
    //出栈元素
    int e;
    bool BOOL;
    cin>>M>>N>>K;
    SqStack *S;
    InitStack(S);
    int List[K][N];
    for(j=0; j<K; j++)
    {
        for(i=0; i<N; i++)
        {
            cin>>List[j][i];
        }
    }
    for(b=0; b<K; b++)
    {
        j=0;
        for(i=1; i<=N; i++)
        {
            BOOL=Push(S,i,M);
            if(!BOOL)
            {
                break;
            }
            //指向需要进行判断的数组
            //当前判断元素等于栈顶元素,就将其出栈,并将指针后移
            while(List[b][j]==S->data[S->top])
            {
                j++;
                Pop(S,e);
            }
        }
        if(S->top==0)
        {
            cout<<"YES"<<endl;
        }
        else
        {
            cout<<"NO"<<endl;
        }
        //清空栈
        S->top=0;
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值