20分求助

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

zyh1145 @ 2024-07-02 21:58:38

不看题解写的,看了题解后发现差不多但是有点出入,想试试我这样彳亍不彳亍

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
#define ll long long 
int q,n,pu[N],po[N];
stack <int> s;
void solve()
{
    while(!s.empty()) s.pop();
    int pui = 1,poi = 1;
    while (poi <= n)
    {
        while (s.empty() || (!s.empty() && s.top() != po[poi]))
        {
            s.push(pu[pui++]);
            //cout<<"s.push("<<s.top()<<")\n";
        } 
        s.pop();
        poi++;
        if (!s.empty() && s.top() != po[poi]) 
        {
            cout<<"No\n";
            return ;
        }
    }
    cout<<"Yes\n";
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>q;
    while (q--)
    {
        cin>>n;
        for (int i = 1;i <= n;i++)
        {
            cin>>pu[i];
        }
        for (int i = 1;i <= n;i++)
        {
            cin>>po[i];
        }
        solve();
    }
    return 0;
}

|