poj_1363_Rai


题目:http://poj.org/problem?id=1363

这个题目题意很简单。给你一组数据,让你判断存在不存在这样的出栈顺序,。

这道题是数据结构一个实验,我选了这个,做一下练习吧!

这个代码是具体栈的实现,

#include<iostream>
using namespace std;
#define size 1010
struct sta
{
	int *top;
	int *base;
	int stasize;
};
int sta_init(sta &s)
{
	s.base=new int [sizeof(int)*size];
	if(!s.base) return 0;
	s.top=s.base;
	s.stasize=size;
	return 1;
}
void sta_push(sta &s,int x)
{
	*s.top++=x;

}
void sta_pop(sta &s)
{
	--s.top;
}
bool sta_e(sta &s)
{
	if(s.top==s.base) return 1;
	return 0;
}

int t[1010];
int main()
{
	int n;
	
	while(cin>>n,n)
	{
		sta S;
		sta_init(S);
		int i,j;
		while(cin>>t[0],t[0])
		{
			while(!sta_e(S))
				sta_pop(S);
			for(i=1;i<n;i++)
				cin>>t[i];
			j=0;
			for(i=1;i<=n;)
			{
				while(sta_e(S) || (!sta_e(S)&&(*(S.top-1)<t[j])))
					sta_push(S,i++);
				while(!sta_e(S)&&(*(S.top-1)==t[j]))
				{ 	j++;sta_pop(S); }
				if(j<n && !sta_e(S) && (*S.top>t[j]))
					break;
			}
			if(sta_e(S))
				cout<<"Yes\n";
			else
				cout<<"No\n";
		}
		cout<<endl;
	}
	return 0;
}

下面这个是调用的库函数。作为参考

#include<stdio.h>
#include <stack>
using namespace std;
int main()
{
	int n;
	while(scanf("%d", &n) && n)
	{
		stack<int> sta;
		int s[1001],i;
		while(scanf("%d", &s[0]))
		{
			while(!sta.empty()) sta.pop();
			if(s[0]==0) { putchar('\n'); break;}
			for(i=1; i<n; i++)
				scanf("%d", &s[i]);
			int j=0;
			for(i=1; i<=n;)
			{
				while(sta.empty() || (!sta.empty() && sta.top()<s[j]))
					sta.push(i++);

				while(!sta.empty() && sta.top()==s[j])
					j++,sta.pop();
				if(!sta.empty() && j<n && sta.top()>s[j])
					break;
			}
			puts(sta.empty()?"Yes":"No");
		}
	}
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值