PAT,其实考察的是对数据结构的运用,毕竟上的陈姥姥的课,日常满分!
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
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct LNode *Stack;
struct LNode{
int Top;
Stack Next;
};
Stack Creat_stack();
void Read_Stack();
bool isEmpty(Stack S);
bool isFull(Stack S, int Captiy);
bool Possibly_result(int M, int N, Stack S);
void Push(int value, Stack S);
void Pop(Stack S);
void Free(Stack S);
void sate_flush(FILE *fp);//清空缓存区
int main()
{
Stack P;
Read_Stack();
return 0;
}
Stack Creat_stack()
{
Stack PtrL;
PtrL = (Stack)malloc(sizeof(struct LNode));
PtrL ->Next = NULL;
return PtrL;
}
void Read_Stack()
{
Stack S;
int M, N, K;
S = Creat_stack();
scanf("%d%d%d", &M, &N, &K);
while(K--)
{
if(Possibly_result(M, N, S)){
printf("YES\n");
}else{
printf("NO\n");
}
}
}
bool Possibly_result(int M, int N, Stack S)
{
int num;
bool sign = false;
scanf("%d", &num);
for(int i = 1; i <= N; i++)
{
Push(i, S);
while(!isEmpty(S) && (S ->Next ->Top) == num){
Pop(S);
if(isEmpty(S) && i == N){
break;
}else{
scanf("%d", &num);
}
}
if(isFull(S, M)){
break;
}
}
sate_flush(stdin);
if(isEmpty(S)){
return true;
}else{
Free(S);
return false;
}
}
void Push(int value, Stack S)
{
Stack P;
P = (Stack)malloc(sizeof(struct LNode));
P ->Top = value;
P ->Next = S ->Next;
S ->Next = P;
}
void Pop(Stack S)
{
Stack T;
if(isEmpty(S))
{
return ;
}else{
T = S ->Next;
S ->Next = T-> Next;
free(T);
return ;
}
}
bool isEmpty(Stack S)
{
return (S ->Next == NULL);
}
bool isFull(Stack S, int Captiy)
{
int num = 0;
if(isEmpty(S))
{
return false;
}
while(!isEmpty(S))
{
S = S ->Next;
num++;
}
if(num == Captiy){
return true;
}else{
return false;
}
}
void Free(Stack S)
{
if(!isEmpty(S)){
while(!isEmpty(S)){
Pop(S);
}
}
return ;
}
void sate_flush(FILE *fp)
{
int ch;
while((ch = fgetc(fp))!= EOF && ch != '\n');
}
这个是大神写的,理论上我的虽然长,但毕竟初学,或许我的也更加适合初学者呢,哈哈
#include<stdio.h>
#include<stdlib.h>
#define Maxsize 1000
typedef struct Node{
int Top;
int Data[Maxsize];
int Capacity;
}*Stack;
int IsPopPossible(int Arry[],int M,int N);
int main()
{
int M = 0,N = 0,K = 0,i = 0,j = 0;
scanf("%d %d %d",&M,&N,&K);
int Arry[N];
for(i = 0;i <= K-1;i++){
for(j = 0;j <= N-1;j++)
scanf("%d",&Arry[j]);
if(IsPopPossible(Arry,M,N))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
int IsPopPossible(int Arry[],int M,int N)
{
int i = 0,flag = 0,count = 0;
Stack Pile = (Stack)malloc(sizeof(struct Node));
Pile->Capacity = M;
Pile->Top = -1;
for(i = 1;i <= N;i++){
if(Pile->Capacity == Pile->Top+1)
return 0;
else
Pile->Data[++Pile->Top] = i;
while(Pile->Data[Pile->Top] == Arry[count]){
Pile->Top--;
count++;
}
}
if(count == N)
return 1;
else
return 0;
}