ZOJ 3210 A Stack or A Queue?(暴力)

本文介绍了一种通过比较输入序列与输出序列来判断输出序列是否符合Stack或Queue特性的算法实现。采用暴力模拟的方法,分别使用Stack和Queue进行对比验证,最终确定输出序列属于哪种数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3210


给出输入和输出序列,判断输出序列是stack还是queue


暴力模拟

#include<iostream>
#include<cstdio>
#include<set>
#include<string>
#include<string.h>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<cctype>
#include<algorithm>
#include<sstream>
#include<utility>
#define mt(a) memset(a,0,sizeof (a))
#define fl(a,b,c) fill(a,b,c)
#define SWAP(a,b,t) (t=a,a=b,b=t)

#define inf 1000000000+7

using namespace std;

stack<int>s;
queue<int>q;
int a[120];

int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		int n;
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			int temp;
			cin >> temp;
			s.push(temp);
			q.push(temp);
		}
		int flag1 = 1,flag2=1;
		for (int i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		for (int i = 0; i < n; i++)
		{
			if (a[i] != q.front())flag1 = 0;
			q.pop();
			if (a[i] != s.top())flag2 = 0;
			s.pop();
		}
		if (flag1&&flag2)cout << "both" << endl;
		else if (flag1)cout << "queue" << endl;
		else if (flag2)cout << "stack" << endl;
		else cout << "neither" << endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值