02-线性结构4 Pop Sequence

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

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.
大意:给你一个能容纳M个元素的栈,元素1到N按升序依次进栈(随机出栈)。给你一个序列,要你判断序列可不可能是这个栈的可出栈序列。例如M=5,N=7。1,2,3,4,5,6,7是一个可能的出栈序列,但是3,2,1,7,5,6,4是不可能的(因为7入栈之前4,5,6一定入栈了,7出栈之后一定是6出栈,而不可能是5)

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

参考AC代码1

#include <iostream>
#include <algorithm>
#include <cmath>
#include <list>
#include <queue>
#include <set>
#include <string>
#include <vector>
#include <map>  //STL 映射容器
#include <memory>
#include <cstdio>  //定义输入输出函数
#include <cstdlib> //定义杂项函数及内存分配函数
#include <cstring>
#define maxn 1004
#define ERROR -1
using namespace std;
typedef struct SNode *PtrToSNode;
typedef  int  ElementType;

struct SNode{
    ElementType Data;
    PtrToSNode Next;
};
typedef PtrToSNode Stack;
Stack CreateStack(){
    /*创建一个堆栈的头节点,返回该节点指针*/
    Stack S;

    S = (Stack) malloc(sizeof(struct SNode));
    S->Data=0;/*用头节点存储栈的元素个数*/
    S->Next = NULL;
    return S;
}
bool IsEmpty( Stack S ){
    /*判断堆栈S是否为空,若是返回true;否则返回false*/
    return (S->Next == NULL);
}
bool Push( Stack S , ElementType X){
    /*将元素X压入堆栈S*/
    PtrToSNode TmpCell;
    S->Data++;
    TmpCell = (PtrToSNode)malloc(sizeof(struct SNode));
    TmpCell->Data=X;
    TmpCell->Next=S->Next;
    S->Next=TmpCell;
    return true;
}
ElementType Pop(Stack S){
    /*删除并返回堆栈S的栈顶元素*/
    PtrToSNode FirstCell;
    ElementType TopElem;

    if(IsEmpty(S)){
        //printf("堆栈空\n");
        return ERROR;
    }
    else{
        FirstCell=S->Next;
        TopElem=FirstCell->Data;
        S->Next=FirstCell->Next;
        free(FirstCell);
        S->Data--;
        return TopElem;
    }
}

ElementType GetTop(Stack S){
    PtrToSNode FirstCell;
    ElementType TopElem;

    if(IsEmpty(S)){
        //printf("堆栈空\n");
        return ERROR;
    }
    else{
        FirstCell=S->Next;
        TopElem=FirstCell->Data;
        return TopElem;
    }
}

int main()
{
    int m,n,k,i,h,temp,a[maxn];
    Stack s ,t;
    //while(scanf("%d%d%d",&m,&n,&k)!=EOF){
        scanf("%d%d%d",&m,&n,&k);
        while(k--){
            s=CreateStack();
            t=CreateStack();
            for(i=0;i<n;i++){
                scanf("%d",&a[i]);
            }
            for(i--;i>=0;i--){
                Push(t,a[i]);
            }
            i=0;
            while(i<=n){
                ElementType t1,t2;
                t1=GetTop(s);
                t2=GetTop(t);
                if(!IsEmpty(s)&&t1==t2){
                    Pop(s);
                    Pop(t);
                }
                else if(i<n&&s->Data<m){
                    i++;
                    Push(s,i);
                }
                else{
                    break;
                }
            }
            if(IsEmpty(s)&&IsEmpty(t)){
                printf("YES\n");
            }
            else{
                printf("NO\n");
            }
        }
    //}
    return 0;
}

参考AC代码2

/* 
    Name:
    Copyright:
    Author: Ambition
    Date: <DATETIME>
    Description:
*/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define maxn 10004 
int main(int argc, char** argv) {
	//freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int M,N,K,a[maxn];
    scanf("%d%d%d",&M,&N,&K);
    int i,j;
    while(K--){
    	stack<int> s,t;
    	for(i=0;i<N;i++){
    		scanf("%d",&a[i]);
		}
		for(i=N-1;i>=0;i--){
			t.push(a[i]);
		}
		int t1,t2;
		i=0;
		while(i<=N){
			
		 	//printf("i=%d\n",i);
		 	
		 	if(!s.empty()){
		 		t1=s.top();
			}
			else t1=-1;
			if(!t.empty()){
				t2=t.top();
			}
			else t2=-1;
			
			//printf("t1=%d t2=%d\n",t1,t2);
			
			if(!s.empty()&&t1==t2){
				s.pop(); 
				t.pop();
				//printf("pop\n");
			}	
			else if(i<N&&s.size()<M){
				i++;
				s.push(i);
				//printf("s.push(%d)\n",i);
			}
			else break;
		}
		if(s.empty()&&t.empty()){
			printf("YES\n");
		}
		else{
			printf("NO\n");
		}
	}
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值