只能过一个案例,大佬求救OvO

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

handless @ 2024-12-27 17:35:06

#include <iostream>
#include <stack>
using namespace std;

const int N =1e5+10;
int a[N],b[N];
stack<int> st;

int main()
{
  int q;
  cin >> q;
  while(q--)
  {
    int n;
    cin >> n;
    for(int i=1;i<=n;i++)
    {
      cin >> a[i];
    }
    for(int i=1;i<=n;i++)
    {
      cin >> b[i];
    }
    int j=1;
    for(int i=1;i<=n;i++)
    {
      st.push(a[i]);
      while(j<=n && st.size() && st.top()==b[j])
      {
        st.pop();
        j++;
      }
    }
    if(st.empty())
    {
      cout << "Yes" << endl;
    }
    else
    {
      cout << "No" << endl;
    }    
  }
  return 0;
}

by diaojunqi @ 2025-01-04 20:05:50

重置栈就?了

#include <iostream>
#include <stack>
using namespace std;

const int N = 1e5 + 10;
int a[N], b[N];
stack<int> st;

int main() {
    int q;
    cin >> q;
    while (q--) {
        int n;
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        for (int i = 1; i <= n; i++) {
            cin >> b[i];
        }
        int j = 1;
        for (int i = 1; i <= n; i++) {
            st.push(a[i]);
            while (j <= n && st.size() && st.top() == b[j]) {
                st.pop();
                j++;
            }
        }
        if (st.empty()) {
            cout << "Yes" << endl;
        } else {
            cout << "No" << endl;
        }
        while (!st.empty()) {
            st.pop();
        }
    }
    return 0;
}

by Star0230 @ 2025-01-04 20:13:53

@diaojunqi this is a joker


by Star0230 @ 2025-01-04 20:15:39

@diaojunqi thanks for your answer!

i can solve this question


|