求助,不知道哪里逻辑错了全WA.....

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

Stevehim @ 2022-09-21 22:07:56

#include <stack>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#define maxn 100010
using namespace std;
int a[maxn];
int b[maxn];
stack<int>s;
int n;
int q;

// 1 5 2 3 4
// 1 5 4 2 3
int main() {
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> n;
        for (int j = 0; j < n; j++) {
            cin >> a[j];
        }
        int sum = 0;
        for (int j = 0; j < n; j++) {
            cin >> b[j];
        }
        for (int j = 0; j < n; j++) { //顺序的
            s.push(a[j]);
            if (a[j] == b[sum]) {
                s.pop();
                sum++;
            }
        }
        bool ss = true;
        int len = n - sum;
        for (int j = 0; j < len; j++) {
//          cout << "j=" << j << endl;
//          cout << s.top() << " " << b[sum] << endl;
            if (s.top() == b[sum]) {
                s.pop();
                sum++;
            } else {
                ss = false;
            }
        }
//      cout << sum << endl;
        if (!s.empty()) {
            ss = false;
        }
        if (ss) {
            cout << "Yes" << endl;
        } else {
            cout << "No" << endl;
        }
    }
    return 0;
}

|