20分求助
查看原帖
20分求助
1029978
zyh1145楼主2024/7/2 21:58

不看题解写的,看了题解后发现差不多但是有点出入,想试试我这样彳亍不彳亍

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
#define ll long long 
int q,n,pu[N],po[N];
stack <int> s;
void solve()
{
	while(!s.empty()) s.pop();
	int pui = 1,poi = 1;
	while (poi <= n)
	{
		while (s.empty() || (!s.empty() && s.top() != po[poi]))
		{
			s.push(pu[pui++]);
			//cout<<"s.push("<<s.top()<<")\n";
		} 
		s.pop();
		poi++;
		if (!s.empty() && s.top() != po[poi]) 
		{
			cout<<"No\n";
			return ;
		}
	}
	cout<<"Yes\n";
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>q;
	while (q--)
	{
		cin>>n;
		for (int i = 1;i <= n;i++)
		{
			cin>>pu[i];
		}
		for (int i = 1;i <= n;i++)
		{
			cin>>po[i];
		}
		solve();
	}
	return 0;
}
2024/7/2 21:58
加载中...