rochcim @ 2021-10-03 13:18:31
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
stack <int> s;
int main () {
int n, x, y;
cin >> n;
while (n--) {
int m;
cin >> m;
while (!s.empty()) s.pop();
for (int i = 1; i <= m; i++) {
scanf("%d", &x);
s.push(x);
}
for (int i = 1; i <= m; i++) {
scanf("%d", &y);
if (y == s.top()) s.pop();
else break;
}
if (s.empty()) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}
by Fish_Clever @ 2021-10-03 13:28:32
你为什么一开始就把所有元素入栈了?
by Fish_Clever @ 2021-10-03 13:30:06
应该是当需要输出某个数时检查一下是否已在栈中,如果是且不在顶部则不可行,在顶部则弹出,不在栈中则不断入栈。
by rochcim @ 2021-10-03 20:06:15
@1093725598yr 自己重写了一下过了,但是请问为什么不能按照我以前的方式写呢?我自己看了一下感觉真没什么问题,就是过不了
by Fish_Clever @ 2021-10-04 12:14:47
1 2 3 4
2 1 4 3