牛客网——贝伦卡斯泰露(栈、队列)(90%通过)

这道题标注是DFS,我正好在练习DFS,就点进去了。但我太菜了,没有看出一点它与DFS的关系,用了队列和栈来做的,题目如下:

我的思路是使用一个栈a来存放输入数据,只有顺序一样才可以满足条件,所以使用队列b来判断是否满足“贝伦卡斯泰露“:当队列b的队头和栈a出队的元素一样时,说明重复序列出现,队列b队头元素出队,反之队伍入队。当最后b为空队时说明满足条件输出“Frederica Bernkastel”,反之不满足条件输出“Furude Rika“。示例代码如下,没有AC但是90%通过,可能有特值没有考虑。

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;

int main(void)
{
	int n,m;
	vector<int> a;
	cin >> m;
	for (int i = 0; i < m; i++)
	{
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			int temp;
			cin >> temp;
			a.push_back(temp);
		}

		queue<int> b;
		while (true)
		{
			if (!b.empty())
			{
				if (!a.empty())
				{
					int temp;
					temp = a.back();
					a.pop_back();

					if (b.front() == temp)
						b.pop();
					else
					{
						b.push(temp);
					}
				}
				else
				{
					cout << "Furude Rika" << endl;
					break;
				}


			}
			else
			{
				if (!a.empty())
				{
					int temp = a.back();
					a.pop_back();
					b.push(temp);
				}
				else
				{
					cout << "Frederica Bernkastel" << endl;
					break;
				}
			}
		}
	}
	

	system("pause");
	return 0;
}
发布了162 篇原创文章 · 获赞 38 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_41938259/article/details/104373651
今日推荐