02-线性结构4 Pop Sequence (25分)

02-线性结构4 Pop Sequence   (25分)

Given a stack which can keep MM numbers at most. Push NN numbers in the order of 1, 2, 3, ..., NN 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 MM is 5 and NN 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): MM (the maximum capacity of the stack), NN (the length of push sequence), and KK (the number of pop sequences to be checked). Then KK lines follow, each contains a pop sequence of NN 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

这题我用了两种方法,一种是不用栈,一种是用栈模拟。感觉自己还是太渣了,代码写的比较长,但是用栈的那种方法思路还是很清晰的,虽然代码略长,但是思路没有问题。

方法一:把n个数按照顺序一行排列,用一个标记数组判断哪些已经出栈,哪些还未出栈,用两个判断函数判断是否满足条件。

#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
#define maxsize 1005
int flag[1001];
int n,m,k;
bool judge(int x,int y)
{
	if(x-y==1)
	{
		return true;
	}
	else
	{
		if(x>y)
		{
			for(int i=y+1;i<x;i++)
			{
				if(!flag[i])
				{
					return false;
				}
			}
			return true;
		}
		if(x<y)
		{
	    	return true;
		}
	}
}
bool amount(int x)
{
	int cnt=0;
	for(int i=1;i<=x;i++)
	{
		if(!flag[i])
			cnt++;
	}
	if(cnt>n-1)
		return false;
	else
		return true;
}
int main()
{
	int a[1001];
	cin>>n>>m>>k;
	while(k--)
	{
		memset(a,0,sizeof(a));
		memset(flag,0,sizeof(flag));
		for(int i=0;i<m;i++)
		{
			cin>>a[i];
		}
		if(a[0]>n)
		{
			cout<<"NO"<<endl;
			continue;
		}
		flag[a[0]]=1;
		int i;
		for(i=0;i<m-1;i++)
		{
			flag[a[i+1]]=1;
			if(!judge(a[i],a[i+1])||!amount(a[i+1]))
			{
				cout<<"NO"<<endl;
				break;
			}
		}
		if(i==m-1)
		{
			cout<<"YES"<<endl;
		}
	}
	return 0;
}
方法二:用栈模拟,栈中元素个数大于限定个数或者栈顶元素不为所要的元素则返回false,否则弹出栈顶元素并返回true。

#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
int n,m,k;
int mm=0;
bool test(stack<int>&s,int x)
{
	if(x>mm)
	{
		for(int i=mm+1;i<=x;i++)
		{
			s.push(i);
		}
		if(s.size()>n||s.top()!=x)
		{
			return false;
		}
		else
		{
			s.pop();
			return true;
		}
	}
	else
	{
		if(s.top()!=x)
		{
			return false;
		}
		else
		{
			s.pop();
			return true;
		}
	}
}
int main()
{
	cin>>n>>m>>k;
	while(k--)
	{
		mm=0;
		stack<int>s;
		int i;
		for(i=0;i<m;i++)
		{
			int temp;
			cin>>temp;
			if(!test(s,temp))
			{
				cout<<"NO"<<endl;
				while(getchar()!='\n');
				break;
			}
			if(temp>mm)
				mm=temp;
		}
		if(i==m)
		{
			cout<<"YES"<<endl;
		}
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没想好叫什么名字

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值