输入样例:
2
5
1 2 3 4 5
5 4 3 2 1
4
1 2 3 4
2 4 1 3
输出样例:
Yes
No
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int a[maxn],b[maxn];
stack<int> s;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<n;i++)
cin>>b[i];
int ind=0;
for(int i=0;i<n;i++)
{
s.push(a[i]);
while(!s.empty()&&s.top()==b[ind])//符合验证的出栈数
{
s.pop();//出栈
ind++;//验证下一个
}
}
if(s.empty())//按验证顺序全部出栈
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
while(!s.empty())//多组样例,清空栈
s.pop();
}
return 0;
}