PAT : 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.

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 #include<stdio.h>
  2 #include<malloc.h>
  3 
  4 /*
  5     * Declare 一个全局数组  (需要一个栈,用全局数组实现)  call it : stack[1001] ;
  6     * Decalre 一个全局变量  : 保存栈顶指针的值;
  7     * Declare 一个全局变量  :保存栈的容量;
  8     * Method  void createStack(int maxSize)  : 规定栈的容量;
  9     * Method  int push(int element)  : 返回 0 (因为出栈的队列不可能为0) 表示栈已满;
 10     * Mehtod  int pop()   : 返回出栈的元素 ((空栈返回 0 )
 11     * Method  int isEmpty() : sh是否已满
 12     //即使stack 为全局数组  ,只能用 push() 和 pop() 访问;
 13 
 14 */
 15  
 16 int stack[1001];
 17 
 18 int top = -1;
 19 int maxSize;
 20 
 21 int  isEmpty()
 22 {
 23     return top == -1;
 24 }
 25 
 26 void createStack(int stackSize)
 27 {
 28     maxSize = stackSize;
 29     top = -1;
 30 }
 31 
 32 int push(int element)
 33 {
 34     if(top == maxSize)
 35         return 0;
 36 
 37     top++;
 38     stack[top] = element;
 39     return 1;
 40 }
 41 
 42 int pop()
 43 {
 44     if(isEmpty())
 45        return 0;
 46      return stack[top--];
 47 }
 48 
 49 int isFull()
 50 {
 51     return top == maxSize-1;
 52 }
 53 
 54 
 55 /* 
 56     *Declear 一全局数组 : stdSeq[1001]   保存标准待进栈的序列 1, 2 ,3 , ····· N 
 57 */
 58 
 59 int stdSeq[1001];
 60 
 61 
 62 /*
 63     method int main
 64         Declare 栈的大小变量             Call it : M 
 65         Declare 待进栈序列的大小的变量    Call it : N
 66         Declare 出栈序列的个数变量        Call it : K
 67 
 68         Input M N K ;
 69 
 70         Declare 一个保存出栈出栈序列是否正确的数组 Call it isPopSeq[1001]; 
 71 
 72         Loop  :  K 个出栈序列
 73             Input 每个序列 input_a[N]
 74             Involved isCorrectSeq(input_a ,N , M )
 75 
 76         输出结果;
 77             
 78     
 79 */
 80 
 81 int isCorrectSeq(int *seq , int seqSize , int stackSzie);
 82 int findIndex(int index);
 83 int main()
 84 {
 85     int M ,N ,K;
 86     scanf("%d",&M);
 87     scanf("%d",&N);
 88     scanf("%d",&K);
 89 
 90     int isPopSeq[1001];
 91     int input_a[1001];
 92 
 93     for(int i = 0; i < K ; i++)  // K 个输入序列
 94     {
 95         for(int j = 0; j < N; j++)
 96         {
 97             scanf("%d",&input_a[j]);
 98         }
 99         isPopSeq[i] = isCorrectSeq(input_a , N , M);
100     }
101 
102     for(int i = 0; i < K; i++)
103     {
104         if(isPopSeq[i])   printf("YES\n");
105         else              printf("NO\n");
106     }
107 
108 }
109 
110 /*
111     Method isCorrectSeq(int *seq , int seqSize , int stackSzie)  
112         Declare Initialize   标准序列 stdSeq[1001]
113             从 下表 1 开始到  N 
114         Initialize 一个栈
115             Involved createStack(stackSize )
116         Loop : for i : seq
117              If  : stdSeq[seq[i]] 未已进栈
118              {
119                  Loop :(将stdSeq[seq[i]] 未进栈的元素全部进栈)
120                     if: 栈满  
121                          return  0;
122                     从右往左查找进栈的位置
123                         依次进栈 ,设stdSeq 对应位置的为0;
124                     
125                  
126                  if 栈空 || 或若栈不空 ,pop()=!std[i]
127                      return 0
128 
129 
130              }
131 
132          return 1;
133 
134 
135 */ 
136 int isCorrectSeq(int *seq , int seqSize , int stackSize)
137 {
138     for(int i =  1 ; i <= seqSize ; i++)     stdSeq[i] = i;
139 
140     createStack(stackSize);
141 
142     for(int i = 0 ;i < seqSize ; i++)
143     {
144         if(stdSeq[seq[i]] != 0)
145         {
146             int index = findIndex(seq[i]); 
147             for( ;index <= seq[i] ; index++)
148             {
149                 if(isFull())
150                     return 0;
151                 push(stdSeq[index]);
152                 stdSeq[index] = 0;
153             }
154         }
155 
156         if(isEmpty() || pop() != seq[i])
157             return 0;
158     }
159     
160     return 1;
161     
162 }
163 
164 int findIndex(int index)
165 {
166     int i = index;
167     for( ; i > 0 && stdSeq[i] != 0; i--) ;
168   
169 return i+1;
170 }

 

posted @ 2018-09-14 16:18  #冰山一角#  阅读(19)  评论(0)    收藏  举报