QiudaoYV @ 2023-11-17 14:04:24
#include <iostream>
#include <stack>
using namespace std;
int main() {
int q,n;
cin>>q;
for(int i=0;i<q; ++i){
int a[100010],p=1,x;
stack <int> b;
cin>>n;
for(int j=0;j<n;++j) cin>>a[j];
b.push(a[0]);
for(int j=0;j<n;++j){
cin>>x;
if((!b.empty())&&b.top()==x) b.pop();
else{
while(a[p]!=x) b.push(a[p++]);
p++;
}
}
if(b.empty()) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
第一个测试点过了,但是其他的都是RE,求大佬指点
by zhutongxuan @ 2024-05-03 16:52:25
if((!b.empty())&&b.top()==x) b.pop();
else{
while(a[p]!=x) b.push(a[p++]);
p++;
}
应改为
if((!b.empty())&&b.top()==x) b.pop();
else{
while(a[p]!=x) b.push(a[p++]);
b.pop();
}
by zhutongxuan @ 2024-05-03 16:57:17
也不对,应该是
#include<bits/stdc++.h>
using namespace std;
int r = 1, q, n, t1[100000], t2[100000];
stack<int> a;
int main() {
cin >> q; while (q--) {
while (!a.empty()) a.pop();
r = 1; cin >> n;
for (int i = 1; i <= n; i++) cin >> t1[i];
for (int i = 1; i <= n; i++) cin >> t2[i];
for (int i = 1; i <= n; i++) {
a.push(t1[i]);
if (!a.empty()) {
while (a.top() == t2[r]) {
a.pop(); r++; if (a.empty()) break;
}
}
}
if (r == n + 1) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}