题解 | 验证栈序列
验证栈序列
https://www.nowcoder.com/practice/d3178fe362dd4810b577c77c9e128fc5
#include <iostream>
#include <stack>
using namespace std;
const int N = 1000010;
int a[N], b[N];
bool judeTryePopStacK(const int *a, const int *b, int length) {
stack<int> s;
int p = 0, q = 0;
for (p; p < length; p++) {
s.push(a[p]);
while (!s.empty() && s.top() == b[q]) {
s.pop();
q++;
}
}
return s.empty();
}
int main() {
int n, length;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> length;
for (int j = 0; j < length; j++) cin >> a[j];
for (int j = 0; j < length; j++) cin >> b[j];
if (judeTryePopStacK(a, b, length)) {
cout << "Yes" << endl;
}else {
cout << "No" << endl;
}
}
return 0;
}
查看17道真题和解析