0分求助,但过样例(玄关

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

STEAMZzR01024602 @ 2024-08-06 17:32:51

#include<bits/stdc++.h>
#define int long long
using namespace std;
int q,n;
stack<int> s;
signed main()
{
    cin>>q;
    while(q--)
    {
        cin>>n;
        int a[n+10],b[n+10];
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            s.push(a[i]);
        }
        for(int i=0;i<n;i++) cin>>b[i];
        for(int i=0;i<n;i++)
        {
            if(s.top()==b[i]) s.pop();
            else
            {
                cout<<"No"<<endl;
                break;
            }
        }
        if(s.empty()) cout<<"Yes"<<endl;
        while(!s.empty()) s.pop();
    }
    return 0;
}

by xzy_AK_IOI @ 2024-08-06 18:54:29

@STEAMZzR01024602
这题不是这么写的,题意不是要将pushed序列读取完再弹出,可以读取部分数据就弹出
AC CODE

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
const int N=1e5+10;
stack <int> s;
int a[N],b[N];
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int _;
    cin>>_;
    int n;
    while (_--){
        cin>>n;
        while (!s.empty()){
            s.pop();
        }
        for (int i=1;i<=n;i++){
            cin>>a[i];
        }
        for (int i=1;i<=n;i++){
            cin>>b[i];
        }
        int zhi=1;
        for (int i=1;i<=n;i++){
            s.push(a[i]);
            while (!s.empty() && b[zhi]==s.top()){
                zhi++;
                s.pop();
            }
        }
        if (zhi!=n+1){
            cout<<"No\n";
        }
        else{
            cout<<"Yes\n";
        }
    }
    return 0;
}

by STEAMZzR01024602 @ 2024-08-06 19:57:09

@xzy_AK_IOI 不愧是小蒸羊!


by STEAMZzR01024602 @ 2024-08-06 19:57:58

此帖结,谢谢


|