不知道哪里出问题了,求大佬指点

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

Exile_Code @ 2023-11-12 16:20:34

#define  _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <list>
#include <string>
#include <cmath>
#include <bitset>
#include <stack>
#include <unordered_set>

#define ull             unsigned long long 
#define ll              long long
#define pii             pair<ll ,ll>
#define ft              first
#define sd              second
#define pr              pair
#define cy              cout<<"Yes"<<endl
#define cn              cout<<"No"<<endl
#define forn(i,n)       for(int (i)=0;(i)<(n);(i)++)   
#define forne(i,n)      for(int (i)=1;(i)<=(n);(i)++)
#define rforn(i,n)      for(int (i)=(n-1);(i)>=(0);(i)--)   
#define rforne(i,n)     for(int (i)=(n);(i)>=(1);(i)--)
#define vv              vector
#define inf             (int)0x004f4f4f;
#define N               (ll)1e5 + 10
ll t;
struct edge {
    int next, to, w;
};

void solve() {

    int n; cin >> n;
    vv<int>nums(n);
    stack<int>st;
    forn(i, n)
        cin >> nums[i];

    int cnt = 0;
    forn(i, n) {
        int a; cin >> a;//当前要出栈的元素
        if (st.size() && st.top() == a)//可以出栈
            st.pop();
        else {
            while(st.empty() || st.top() != a) {//向后找这个可能出栈的元素
                //栈空或者栈顶不等a
                if (cnt >= n){
                    cout << "No" << endl;
                    return;
                }
                st.push(nums[cnt++]);//将后面的入栈
            }
            st.pop();//一定是找到了一个等于当前要出栈的元素
        }
    }
    cout << "Yes" << endl;

}
int main() {

    //t = 1;
    cin >> t;

    while (t--)
        solve();

    return 0;
}

|