PAT——A1051 Pop Sequence(stack)

题目链接:

#include<utility>
#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
using namespace std;
#define maxn 1010
int arr[maxn];
stack<int >st;
int main()
{
    int t,m,n;
    scanf("%d%d%d",&t,&m,&n);
    while(n--)
    {
        while(st.empty()!=true)
        {
            st.pop();
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&arr[i]);
        }
        int current=1;
        bool flag=true;
        for(int i=1;i<=m;i++)
        {
            st.push(i);
            if(st.size()>t)
            {
                flag=false;
                break;
            }
           while(!st.empty()&&st.top()==arr[current])
           {
              st.pop();
              current++;
           }
        }
        if(st.empty()&&flag==true)
        {
            printf("YES\n");
        }
        else
            printf("NO\n");
    }
    return 0;
}

理解题意之后还蛮简单的

其实就是判断出栈顺序是否可能

就是模拟

每个数入栈顺序是否能等于出栈顺序

但是要注意

每次判断的时候

都要清空上一次的栈(判断非空)

还有就是

入栈的容量是否超过了题目所定义的

猜你喜欢

转载自blog.csdn.net/qq_42232118/article/details/82082177