洛谷 P4387
验证栈序列
AC代码
#include<bits/stdc++.h>
using namespace std;
int main() {
int q; cin >> q;
for (int i = 0; i < q; i++) {
int n, x; cin >> n;
stack<int> pushed;
vector<int> a, b;
for (int j = 0; j < n; j++) {
cin >> x;
a.push_back(x);
}
for (int j = 0; j < n; j++) {
cin >> x;
b.push_back(x);
}
int count = 0;
for (int j = 0; j < n; j++) {
pushed.push(a[j]);
while (pushed.top() == b[count]) {
pushed.pop(); count++;
if (pushed.empty()) break;
}
}
if (pushed.empty()) cout << "Yes" << endl;
else cout << "No" << endl;
}
}