算法感觉没问题,但是wa一片

P4387 【深基15.习9】验证栈序列

Czh116489 @ 2023-11-10 21:55:19

include<iostream>

include<stack>

include<vector>

using namespace std;

int main() {

int 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;

}

}


by heyx0201 @ 2023-11-10 22:18:29

@Czh116489

数据千万条,清空第一条。

多测不清空,爆零两行泪。

清空不规范,超时总相伴。


by heyx0201 @ 2023-11-10 22:20:17

@Czh116489

#include <iostream>
#include <stack>
#include <vector>

using namespace std;

int main() {
  int ask_num = 0;

  cin >> ask_num;
  int stack_length = 0;
  while (ask_num--) {
    cin >> stack_length;
    vector<int> pushed;
    vector<int> poped;
    stack<int> tmp_stack;
    // 实现插入栈
    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;
  }
}

by Czh116489 @ 2023-11-10 22:20:22

@heyx0201 虽然但是是wrong answer


by heyx0201 @ 2023-11-10 22:20:59

@Czh116489 你WA就是清空问题。。。


by Czh116489 @ 2023-11-10 22:22:23

@he @heyx0201 老大我去看看


by Czh116489 @ 2023-11-10 22:24:43

@heyx0201 老大还是蛙声一片


by heyx0201 @ 2023-11-10 22:26:55

@Czh116489 我不是回复了你一个代码吗


by heyx0201 @ 2023-11-10 22:27:07

@Czh116489 那个是对的啊


by Czh116489 @ 2023-11-10 22:31:27

@heyx0201 为什么放进去就ac了啊,老大


by Czh116489 @ 2023-11-10 22:32:53

@heyx0201 我tm我忘记了,定义在循环外来草


|