02-线性结构4 Pop Sequence

Posted shin0324

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了02-线性结构4 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
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 struct SNode{
 4     int *data;
 5     int top;
 6     int maxsize;
 7 };
 8 typedef struct SNode *stack;
 9 
10 //初始化堆栈函数 
11 stack createstack(int maxsize){
12     stack S = (stack)malloc(sizeof(struct SNode));
13     S->data = (int*)malloc(sizeof(int)*maxsize);
14     S->top = -1;
15     S->maxsize = maxsize;
16     return S;
17 }
18 
19 //判断堆栈是否为空函数 
20 bool Isempty(stack S){
21     return (S->top == -1);
22 }
23 
24 //入栈函数 
25 void push(stack S, int temp) {
26     S->data[++(S->top)] = temp;
27 }
28 
29 //出栈函数 
30 void pop(stack S){
31     if(S->top==-1) return;
32     else {
33         S->top--;
34     }
35 }
36 
37 int main( ){
38     int M, N, K;                 
39     scanf("%d%d%d", &M, &N, &K);
40     stack S = createstack(M);
41     while(K--){
42         bool flag = true;         
43         int temp = 1, num;
44         for(int i=0; i<N; i++){                //循环读入push sequence的每个数 
45             scanf("%d", &num);
46             while(S->top<S->maxsize && flag)          //当堆栈不满,且前面的元素均是可能的出栈序列时 
47             {
48                 if(Isempty(S) || S->data[S->top]!=num)   //若堆栈空,或栈顶元素不等于num 
49                 {
50                     push(S, temp++);                 //将temp按升序压入堆栈 
51                 }
52                 else if(S->data[S->top]==num)        //直到栈顶元素等于num 
53                 {
54                     pop(S);                          //将元素出栈 
55                     break;                          
56                 }
57             }
58             if(S->top>=S->maxsize)       //如果temp一直压到堆栈满,仍未找到符合num的出栈元素 
59             {
60                 flag = false;            //说明该序列不是可能的出栈序列,将flag设为false 
61             }
62         }
63         if(flag) printf("YES
");     
64         else printf("NO
");
65         while(!Isempty(S))    //每个序列判断完,将堆栈清空 
66         {
67             pop(S);
68         }
69     }
70     return 0;    
71 }

 

以上是关于02-线性结构4 Pop Sequence的主要内容,如果未能解决你的问题,请参考以下文章

02-线性结构4 Pop Sequence

数据结构 02-线性结构4 Pop Sequence

线性结构4 Pop Sequence

02-线性结构4 Pop Sequence(PTA数据结构题)

数据结构线性结构 —— 编程作业 04 :Pop Sequence

数据结构专题 ——栈的应用 A1051.Pop Sequence(25)