02-线性结构4. Pop Sequence(25)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/whzyb1991/article/details/46663867

pat-mooc-02-4

题目的意思:
栈的大小M,输入序列的长度N(默认序列即为1,2,3…,N),入栈出栈的顺序不定。有K个测试序列,判断每一个测试序列是否是可能的出栈顺序。
核心:从出栈顺序推测入栈顺序

关键点:当看到pop t时,那么一定要先push 1,2,3…t-1,t,即要先将小于等于**t的数都push入栈才能pop t。这个过程中检查**pop的次数是否等于N以及入栈的数目是否大于栈的容量M

#include<stack>
#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>

using namespace std;
int M,N,K;
int check_stack(vector<int> &v){
    stack<int> sta;
    sta.push(0);    //dummy element, as sential
    int cap = M + 1;    //stack capacity
    int idx = 0;        //index of vector v
    int num = 1;        //to push to stack
    while(idx != N){
        while(v[idx]>sta.top()&&sta.size()<cap&&idx<=N)
            sta.push(num++);        
        if(v[idx]==sta.top()){
            sta.pop();
            idx++;
        }
        else //sta.size>=cap; idx>N
            return 0;   //false
    }
    return 1;   //true
}
int main(){
    //int M,N,K;
    cin>>M>>N>>K;
    vector<int> vec(N,0);   //N pop number
    for(int i = 0; i!=K; ++i){
        //input to vec
        copy_n(istream_iterator<int>(cin),N,vec.begin());
        cout << (check_stack(vec)?"YES":"NO")<<endl;
    }   
}

这里写图片描述
语言技巧
copy_n(istream_iterator(cin),N,vec.begin());

当然也可以自己用c,写一下栈的操作,速度上会快不少

#include<stdlib.h>
#include<stdio.h>

#define MaxSize 1000
//Stack implement with array
typedef struct node{
        int Data[MaxSize];
        int cap;
        int top;        //size = top+1
}Stack;

int M,N,K;
Stack *CreateStack(){
        Stack *PtrS = (Stack*)malloc(sizeof(struct node));
        PtrS->cap = MaxSize;
        PtrS->top = -1;
        return PtrS;
}

void Push(Stack *PtrS, int ele){
        if(PtrS->top == PtrS->cap-1){
                printf("FULL");
                return;
        }
        PtrS->top++;
        PtrS->Data[PtrS->top]=ele;
}

int top(Stack *PtrS){
        return PtrS->Data[PtrS->top];
}

void Pop(Stack *PtrS){
        if(PtrS->top==-1){
                printf("Empty");
                return;
        }
        PtrS->top--;
}

int check_stack(int v[]){
        int new_cap = M+1;
        Stack *ps = CreateStack();
        Push(ps,0);     //dummy element
        int idx = 0;    //index of v
        int num=1;      //to put to stack
        while(idx!=N){
                while(top(ps)<v[idx]&&idx!=N&&(ps->top+1)<new_cap)
                        Push(ps,num++);
                if(top(ps)==v[idx]){
                        Pop(ps);
                        idx++;
                }
                else
                        return 0;       //false
        }
        return 1;

}

int main(){
        //输入
        scanf("%d %d %d",&M,&N,&K);
        int *v = (int *)malloc(sizeof(int)*N);
        int i;
        for(;K!=0;--K){
                for(i = 0; i!= N; ++i)
                        scanf("%d",v+i);
                if(check_stack(v))
                        printf("YES\n");
                else
                        printf("NO\n");
        }
        return 0;
}

这里写图片描述

阅读更多
换一批

02-线性结构4. Pop Sequence (25)。。第一个测试点一直不过。。求助!!

07-25

[code=c]/*rn02-线性结构4. Pop Sequence (25)rnrn时间限制rn100 msrn内存限制rn65536 kBrn代码长度限制rn8000 Brn判题程序rnStandardrn作者rnCHEN, YuernGiven 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.rnrnInput Specification:rnrnEach 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.rnrnOutput Specification:rnrnFor each pop sequence, print in one line "YES" if it is indeed a possible pop sequence of the stack, or "NO" if not.rnrnSample Input:rn5 7 5rn1 2 3 4 5 6 7rn3 2 1 7 5 6 4rn7 6 5 4 3 2 1rn5 6 4 3 7 2 1rn1 7 6 5 4 3 2rnSample Output:rnYESrnNOrnNOrnYESrnNOrnrn*/rnrn#include rnusing namespace std;rnrnclass Stackrn private:rn int * i_stack;rn int top;rn int maxnum;rn public:rn Stack(int maxnum)rn i_stack =new int[maxnum];rn this->maxnum=maxnum;rn top=-1;rn rn int pop()rn return i_stack[top--];rn rn bool push(int num)rn top++;rn if(isFull())rn return false;rn rn i_stack[top]=num;rn return true;rn rn bool isFull()rn return top==maxnum;rn rn void clear()rn top=-1;rn rn bool isEmpty()rn return top==-1;rn rn;rnrnint main()rn int m,n,k;rn cin>>m>>n>>k;rn int num[n];rn bool err[m];rn int flag=0;rn for(int i=0;i>num[i];rn rn for(int i=0;inum[i-1])rn for(int j=0;flag!=num[i];j++)rn if(!stack.push(++flag))rn err[t]=true;rn break;rn rn rn stack.pop();rn rn elsern int temp;rn temp=stack.pop();rn if(temp!=num[i])rn err[t]=true;rn break;rn rn rn rn if(!stack.isEmpty())rn err[t]=true;rn rn rn rn for(int i=0;i

没有更多推荐了,返回首页