7-3-Pop Sequence-编程题

7-3-Pop Sequence-编程题

解题代码

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int M, N, K, i, j;
	scanf("%d %d %d", &M, &N, &K);
	for (i = 0; i < K; i++) {
		int *a = (int *)malloc(N * sizeof(int));
		int *b = (int *)malloc((N + 1) * sizeof(int));
		for (j = 0; j < N; j++) scanf("%d", &a[j]);
		int top = -1, ptr = 0, instack = 1;
		while (ptr < N) {
			if (instack == a[ptr] && top < M - 1) {
				ptr++;
				instack++;
			}
			else if (top > -1 && a[ptr] == b[top]) {
				top--;
				ptr++;
			}
			else if (top < M-2 && ptr < N) {
				b[++top] = instack++;
			}
			else break;
		}
		if (ptr < N) printf("NO\n");
		else printf("YES\n");
	}
	return 0;
}

测试结果

在这里插入图片描述

问题整理

1.这题的NMK不要弄混。
2.这题的分类思想值得学习。
发布了47 篇原创文章 · 获赞 2 · 访问量 1332

猜你喜欢

转载自blog.csdn.net/Aruasg/article/details/105217306