1051 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

 题目链接:https://www.patest.cn/contests/pat-a-practise/1051

C语言实现代码如下:

  1 #include<stdio.h>
  2 #include<malloc.h>
  3 #define TYPE int
  4 #define EMPTY -1
  5 
  6 typedef struct Stack SqStack;
  7 typedef SqStack *PtrStack;
  8 struct Stack{
  9     TYPE *Array;        //pointer to element array
 10     int Top;            //top of stack
 11     int Maxsize;        //the max size of stack array
 12 }; 
 13 
 14 void PopSequence( TYPE* source, TYPE* Sequence, int N, int M);
 15 void CreateStack( PtrStack* pS, int Maxsize );
 16 void InitStack( PtrStack S );
 17 void push( PtrStack S, TYPE e );
 18 TYPE pop( PtrStack S );
 19 int IsEmpty( PtrStack S );
 20 int IsFull( PtrStack S );
 21 
 22 int main(){
 23     int M,N,K,i_N,i_K;
 24     TYPE *source,**Sequence;  /*Sequence 二维动态数组*/
 25     
 26     scanf( "%d %d %d",&M,&N,&K );
 27     source = (TYPE*) malloc (sizeof(TYPE) * N);
 28     for( i_N=0; i_N<N; i_N++ ){
 29         source[i_N] = i_N + 1;
 30     }
 31     Sequence = (TYPE**) malloc ( sizeof(TYPE*) * K );
 32     
 33     for( i_K=0; i_K<K; i_K++ ){
 34         Sequence[i_K] = (TYPE*) malloc ( sizeof(TYPE) * N );
 35         for( i_N=0; i_N<N; i_N++ ){
 36             scanf( "%d",&Sequence[i_K][i_N] );
 37         }
 38     }
 39     for( i_K=0; i_K<K; i_K++ ){
 40         PopSequence( source, Sequence[i_K], N, M );
 41         free( (void*) Sequence[i_K] );
 42     }
 43     
 44     free( Sequence );
 45     free( source );
 46     
 47 
 48     return 0;
 49 }
 50 
 51 /* flag=0:YES,flag=1:NO */
 52 /*传入source sequence和要判断是否可能为possible pop sequence 的Seq
 53 以及N number和Stack Maxsize M*/
 54 void PopSequence( TYPE* source, TYPE* Seq, int N, int M ){
 55     int i_src=0;
 56     int i_seq=0;
 57     int tmp_pop;
 58     int flag = 0;
 59     PtrStack S;
 60     CreateStack( &S, M );
 61 
 62 /*循环条件,堆栈非空或source sequence还有元素*/
 63     while( i_src < N || !IsEmpty( S ) ){
 64         
 65         /*若source元素大于或等于待判断序列元素,则入栈,
 66         若栈满则置位flag并break*/
 67         if(  i_src < N && Seq[i_seq] >=source[i_src]  ){
 68             if( IsFull( S ) ){
 69                 flag = 1;
 70                 break;
 71             }
 72             push( S, source[i_src++] );
 73         }
 74         
 75         /*若source元素已经遍历完或其大于待判断序列元素,则出栈,
 76         若不等则置位flag并break*/
 77         if( i_src >= N || source[i_src] > Seq[i_seq] ){
 78             tmp_pop = pop( S );
 79             if( Seq[i_seq] != tmp_pop ){
 80                 flag = 1;
 81                 break;
 82             }
 83             i_seq++;
 84         }
 85     }
 86     
 87     if( !flag ){
 88         printf( "YES\n" );
 89     }
 90     if( flag ){
 91         printf( "NO\n" );
 92     }
 93     free( S->Array );
 94     free( S );
 95 }
 96 
 97 /*创建堆栈并初始化*/
 98 void CreateStack( PtrStack* pS, int Maxsize ){
 99     *pS = ( PtrStack ) malloc( sizeof( SqStack ) );
100     (*pS)->Maxsize = Maxsize;
101     InitStack( *pS );
102     (*pS)->Array = ( TYPE* ) malloc( sizeof( TYPE )* Maxsize );
103 }
104 
105 void InitStack( PtrStack S ){
106     S->Top = EMPTY;
107 }
108 
109 /*入栈*/ 
110 void push( PtrStack S, TYPE e ){
111     if( IsFull( S ) ){
112         printf( "The Stack is Full\n");
113     }
114     else{
115         S->Array[++S->Top] = e;
116     }
117 }
118 
119 /*出栈并返回栈顶元素*/ 
120 TYPE pop( PtrStack S ){
121     if( IsEmpty( S ) ){
122         printf( "The Stack is Empty\n" );
123     }
124     else{
125         return S->Array[S->Top--];        
126     }    
127 }
128 
129 /*判栈空*/ 
130 int IsEmpty( PtrStack S ){
131     int ret = 0;
132     if( S->Top  == EMPTY ){
133         ret = 1;
134     }
135     return ret;
136 }
137 
138 /*判栈满 if full return 1*/
139 int IsFull( PtrStack S ){
140     int ret = 0;
141     if( S->Top >= S->Maxsize - 1 ){
142         ret = 1;
143     }
144     return ret;
145 }

 

posted @ 2017-03-30 17:33  yclzju  阅读(234)  评论(0编辑  收藏  举报