求助QZ

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

lbc20070331 @ 2023-08-13 20:39:57

#include<bits/stdc++.h>
using namespace std;
int t;
int n;
int main()
{
    cin>>t;
    while(t--)
    {
        stack<int> in,out;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            int x;
            cin>>x;
            in.push(x);
        }
        for(int i=1;i<=n;i++)
        {
            int x;
            cin>>x;
            out.push(x);
            while(true)
            {
                if(in.top()==out.top()) out.pop(),in.pop();
                if(in.size()==0 || out.size()==0 || in.top()!=out.top()) break;
            }
        }
        if(out.size()!=0 || in.size()!=0) cout<<"No"<<"\n";
        else cout<<"Yes"<<"\n";
    }
    return 0;
}

by ShanireZ @ 2023-08-14 08:12:34

可以在入的过程中出。。。。


by lbc20070331 @ 2023-08-14 21:02:43

改成用数组模拟栈后过了,但我还是觉得我原来写的挺对的


|