求助!求助!求助!!!

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

贾景铄 @ 2023-05-26 21:32:32

#include<iostream>
#include<stack>
using namespace std;
int main()
{
    stack<int> what;
    int q;
    int n;
    int x;
    cin>>q;
    for(int i=1;i<=q;i++)
    {
        cin>>n;
        int flag=0;
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            what.push(x);
        }
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            if(x!=what.top())
            {
                flag=1;
            }
            what.pop();
        }
        if(flag==0)
        {
            cout<<"Yes"<<endl;
        }
        else
        {
            cout<<"No"<<endl;
        }
        while(!what.empty())what.pop();
    }
    return 0; 
}

by lihefan @ 2023-06-03 11:41:54

@贾景铄

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

const int N = 100010;
int a[N], b[N];

int main()
{
    int q;
    cin >> q;
    while (q -- )
    {
        int n;
        cin >> n;
        for (int i = 0; i < n; i ++ ) cin >> a[i];
        for (int i = 0; i < n; i ++ ) cin >> b[i];

        stack<int> stk;
        int j = 0;
        for (int i = 0; i < n; i ++ )
        {
            stk.push(a[i]);
            while (stk.size() && stk.top() == b[j])
            {
                stk.pop();
                j ++ ;
            }
        }

        if (stk.empty()) puts("Yes");
        else puts("No");
    }

    return 0;
}

|