0分help!

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

_Geburah @ 2024-06-15 20:42:37


#include<iostream>
using namespace std;
int b[100001],c[100001];
struct stack
{
    int a[100001];
    int sum=1;
    int size()
    {
        return sum-1;
    }
    bool empty()
    {
        if(sum==0)return 0;
        else return 1;
    }
    void push(int x)
    {
        a[sum]=x;
        sum++;
    }
    void pop()
    {
        sum--;
    }
    int top()
    {
        return a[sum];
    }
    void clear()
    {
        a[sum]=0;
        while(sum--)
        {
            a[sum]=0;
        }
    }
};
stack a;
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int m;
        cin>>m;
        int top=1,under=1;
        for(int j=1;j<=m;j++)
        {

            cin>>b[j];
        }
        for(int j=1;j<=m;j++)
        {
            cin>>c[j];
        }
        for(int j=1;j<=m;j++)
        {
            a.push(b[j]);
            if(a.top()==c[under])
            {
                under++;
                a.pop();
            }
            while(a.top()==c[under])
            {
                a.pop();
                under++;
                if(a.sum==1)
                {
                    break;
                }
            }
        }
        if(a.sum==1)cout<<"Yes\n";
        else cout<<"No\n";
    }
    return 0;
}

by canwen @ 2024-06-17 21:58:59

复杂了

#include<iostream>
#include<stack>
using namespace std;
#define ll long long
stack <ll> s;
ll a[100001],b[100001];
int main(){
    int t;
    cin>>t;
    while(t--){
        int l,q=1;cin>>l;
        for(int i=1;i<=l;i++) cin>>a[i];
        for(int i=1;i<=l;i++) cin>>b[i];
        for(int i=1;;){
            if(!s.empty()&&s.top()==b[q]){
                s.pop();q++;
                if(q==l+1) break;
            }else{
                if(i==l+1) break;
                s.push(a[i]);
                i++;
            }
        }
        if(s.empty()) cout<<"Yes\n";
        else cout<<"No\n";
        while(!s.empty()) s.pop();
    }
    return 0;
}

|