社区讨论
算法感觉没问题,但是wa一片
P4387【深基15.习9】验证栈序列参与者 2已保存回复 10
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 10 条
- 当前快照
- 1 份
- 快照标识符
- @losomkgx
- 此快照首次捕获于
- 2023/11/10 21:57 2 年前
- 此快照最后确认于
- 2023/11/11 08:48 2 年前
CPP
#include
#include
#include
using namespace std;
int main() {
CPPint ask_num = 0;
cin >> ask_num;
vector<int> pushed;
vector<int> poped;
stack<int> tmp_stack;
int stack_length = 0;
while (ask_num--) {
cin >> stack_length;
// 实现插入栈
for (int i = 0; i < stack_length; i++) {
int num;
cin >> num;
pushed.push_back(num);
}
for (int i = 0; i < stack_length; i++) {
int num;
cin >> num;
poped.push_back(num);
}
// 开始判断出栈是否为yes
int j = 0, top = 0;
for (int i = 0; i < stack_length; i++) {
tmp_stack.push(pushed[i]);
while (!tmp_stack.empty() && tmp_stack.top() == poped[j]) {
tmp_stack.pop();
j++;
}
}
if (tmp_stack.empty())
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
回复
共 10 条回复,欢迎继续交流。
正在加载回复...