1051. Pop Sequence

原题连接:https://www.patest.cn/contests/pat-a-practise/1051

题目:

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

这道题我参阅了 http://blog.csdn.net/whzyb1991/article/details/46663867 这篇博文的思路,稍有改动,并且我是用链式存储堆栈,而博文的作者是用顺序存储的方式,读者可以对比两种
方式的优缺点。
这道题并不是有多复杂,关键在于想出it is not a possible pop sequence of the stack的条件!我是苦于没有思路,才参阅了博主的文章。共勉!
我的代码:
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<stdbool.h>
 4 #define Max 1000
 5 typedef struct SNode{
 6     int Data;
 7     struct SNode *Next;
 8 }Stack;
 9 
10 Stack *CreateStack()
11 {
12     Stack *Ptrs=(Stack*)malloc(sizeof(struct SNode));
13     Ptrs->Next=NULL;
14     return Ptrs;
15 }
16 
17 bool IsEmpty(Stack *Ptrs)
18 {
19     return(Ptrs->Next==NULL);
20 }
21 
22 void Push(Stack *Ptrs,int X)
23 {
24     Stack *S;
25     S=(Stack *)malloc(sizeof(struct SNode));
26     S->Data=X;
27     S->Next=Ptrs->Next;
28     Ptrs->Next=S;
29 }
30 
31 void Pop(Stack *Ptrs)
32 {
33     if(IsEmpty(Ptrs)) return;
34     Stack *FirstCell;
35     FirstCell=Ptrs->Next;
36     Ptrs->Next=FirstCell->Next;
37     free(FirstCell);
38 }
39 /* 计算堆栈的长度 */
40 int Length(Stack *Ptrs)
41 {
42     Stack *p;
43     p=Ptrs->Next;
44     int cnt =0;
45     while(p)
46     {
47         cnt++;
48         p=p->Next;
49     }
50     return cnt;
51 }
52 /* 检验每一line是否符合要求 */
53 int check_stack(int *v,int N,int M)
54 {
55     int i=0;
56     int num=1;
57     Stack*ps;
58     ps=CreateStack();
59     Push(ps,0);  //先给栈中压入一个元素0
60     while(i<N)   //审核每个已给元素
61     {  /* 核心 */  //入栈条件;当前栈顶元素小于已给数字,并且(前提条件)栈内元素的总容量小于栈的容量
62         while(ps->Next->Data<v[i] && Length(ps)<M+1)
63             Push(ps,num++);     //压入数num后,由于"the order is 1,2,……N。故下一个压入的数必为num+1;
64         if (ps->Next->Data==v[i])
65         {
66             Pop(ps);   //栈顶元素出栈
67             i++;       //之后的栈顶元素和下一个已给元素继续比较
68         }
69         else
70         {
71             free(ps);
72             return 0;}   //False!
73     }
74     free(ps);
75     return 1;
76 
77 }
78 int main()
79 {
80     int M,N,K;
81     scanf("%d %d %d",&M,&N,&K);
82 
83     int *v=(int *)malloc(sizeof(int)*N);
84     int i,j;
85     for(i=0;i<K;i++)
86     {
87         for (j=0;j<N;j++)
88         {
89             scanf("%d",v+j);
90         }
91         if(check_stack(v,N,M))printf("YES\n");
92         else printf("NO\n");
93     }
94     free(v);
95     return 0;
96 
97 }

 

posted @ 2016-08-16 19:09  变通无敌  阅读(176)  评论(0编辑  收藏  举报