代码如下
#include<bits/stdc++.h>
#define Y "Yes"
#define N "No"
using namespace std;
int q;
int main()
{
//s1输入,s3目标结果,s2中间栈
stack<int> s1,s2,s3;
cin >> q;
for(int i = 1;i <= q;i++)
{
int n = 0;
cin >> n;
for(int j = 1; j <= n;j++)
{
int t = 0;
cin >> t;
s1.push(t);
}
for(int j = 1;j <= n;j++)
{
int t = 0;
cin >> t;
s3.push(t);
}
while(1)
{
//目标结果栈为空则成功
if(s3.empty()){ cout << Y << endl; break; }
int t = 0;
if(!s1.empty())
{
t = s1.top();
s2.push(s1.top());
s1.pop();
}
if(!s2.empty())
{
if(s1.empty())
{
//若输入栈空,中间栈顶不等于目标栈顶,就等于失败了
if(s2.top() != s3.top())
{
cout << N << endl;
break;
}
}
if(s2.top() == s3.top())
{
s3.pop();
s2.pop();
}
}
}
}
return 0;
}